didSelectRowAtIndexPath 上的变量清空或擦除

发布于 2024-12-21 20:24:00 字数 4846 浏览 0 评论 0原文

由于某种原因,我无法在以下代码中的第一个 IF 语句之后访问任何变量。例如,如果索引路径为[0,0],则变量phoneText 会输出电话号码。但如果它是 [1,0] 或 [2,0],我会得到“null”返回。为什么我的变量被删除了?

mapviewcontroller.m 中的以下函数设置值。我实际上这里有一个错误,显示“未找到实例方法 setDetails”。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    //this determines what kind of item was selected
    if ([control isKindOfClass:[UIButton class]]) {

        NSLog(@"Trying to load VenueIdentifier...");

        FinderAnnotation *clicked = view.annotation;      

        FinderViewController *fvi = [self.storyboard instantiateViewControllerWithIdentifier:@"FinderDetail"];

        NSString* latitude = [NSString stringWithFormat:@"%f",clicked.coordinate.latitude];
        NSString* longitude = [NSString stringWithFormat:@"%f",clicked.coordinate.longitude];

        NSLog(@"lat: %@",latitude);
        NSLog(@"lon: %@",longitude);

        [fvi setDetails:clicked.title phone:clicked.phone address:clicked.address beersavailable:clicked.beersavailable latitude:latitude longitude:longitude];

        [self.navigationController pushViewController:fvi animated:YES];

    }
}

然后我的finderdetail.h创建这些变量:

@interface FinderDetail : UITableViewController{

    UITableViewCell *phone;
    UITableViewCell *address;
    UITableViewCell *directions;
    UILabel *venueLabel;

    NSString *phoneText;
    NSString *addressText;
    NSString *venueText;
    NSString *beersavailable;

    NSString *latitudeText;
    NSString *longitudeText;
    }

@property (nonatomic, retain) IBOutlet UITableViewCell *phone;
@property (nonatomic, retain) IBOutlet UITableViewCell *address;
@property (nonatomic, retain) IBOutlet UITableViewCell *directions;
@property (nonatomic, retain) IBOutlet UILabel *venueLabel;

@property (nonatomic, retain) NSString *phoneText;
@property (nonatomic, retain) NSString *addressText;
@property (nonatomic, retain) NSString *venueText;
@property (nonatomic, retain) NSString *beersavailble;
@property (nonatomic, retain) NSString *latitudeText;
@property (nonatomic, retain) NSString *longitudeText;


@end

最后,finderdetail.m获取这些值,将它们分配给变量,并将它们吐入表中:

@implementation FinderDetail

@synthesize venueLabel, phone, address, directions;
@synthesize phoneText, addressText, venueText, beersavailble, latitudeText, longitudeText;
NSString *notlisted;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)setDetails:(NSString *)v phone:(NSString *)p address:(NSString *)a beersavailable:(NSString *)ba latitude:(NSString *)lat longitude:(NSString *)lon
{
    NSLog(@"venue: %@",v);
    NSLog(@"phone: %@",p);
    NSLog(@"address: %@",a);
    NSLog(@"beersavailable: %@",ba);
    NSLog(@"%@",lat);
    NSLog(@"%@",lon);

    latitudeText = lat;
    longitudeText = lon;  
    phoneText = p;
    addressText = a;
    venueText = v;
    beersavailble = ba;


    NSLog(@"%@", latitudeText);
    NSLog(@"%@", longitudeText);


    notlisted = @"Not Listed";
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"Latitude: %@", latitudeText);
    NSLog(@"Longitude: %@", longitudeText);


    phone.detailTextLabel.text = phoneText;
    address.detailTextLabel.text = addressText;
    self.venueLabel.text = venueText;

    if(phoneText == nil){
        phone.detailTextLabel.text = notlisted;
    }

    if(addressText == nil){
        address.detailTextLabel.text = notlisted;
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if(section ==0)
        return 1;
    else
    if(section ==1)
        return 1;
    else
    if(section ==2)
        return 1;
    else
    return 0;
}
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"%@",indexPath);

        if((indexPath.section==0) && (indexPath.row ==0))
        {
            NSLog(@"%@",phoneText);
        }

        if((indexPath.section==1) && (indexPath.row ==0))
        {
            NSLog(@"%@",addressText);
        }

        if((indexPath.section==2) && (indexPath.row ==0))
        {
            NSLog(@"%@",latitudeText);
            NSLog(@"%@",longitudeText);
        }
    }

初始phoneText将显示在NSLog中,但addressText、latitudeText和longitudeText返回null。我可以将phoneText 放入其中一个较低的if 语句中,它也返回null。谢谢!!!

For some reason, I can't access any of my variables after the first IF Statement in the following code. For instance, if index path is [0,0], then the variable phoneText spits out a phone number. But if its [1,0] or [2,0], I get a "null" return. Why is my variable being erased?

The following function in mapviewcontroller.m sets the values. I do actually have an error here that says "instance method setDetails not found".

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    //this determines what kind of item was selected
    if ([control isKindOfClass:[UIButton class]]) {

        NSLog(@"Trying to load VenueIdentifier...");

        FinderAnnotation *clicked = view.annotation;      

        FinderViewController *fvi = [self.storyboard instantiateViewControllerWithIdentifier:@"FinderDetail"];

        NSString* latitude = [NSString stringWithFormat:@"%f",clicked.coordinate.latitude];
        NSString* longitude = [NSString stringWithFormat:@"%f",clicked.coordinate.longitude];

        NSLog(@"lat: %@",latitude);
        NSLog(@"lon: %@",longitude);

        [fvi setDetails:clicked.title phone:clicked.phone address:clicked.address beersavailable:clicked.beersavailable latitude:latitude longitude:longitude];

        [self.navigationController pushViewController:fvi animated:YES];

    }
}

Then my finderdetail.h creates these variables:

@interface FinderDetail : UITableViewController{

    UITableViewCell *phone;
    UITableViewCell *address;
    UITableViewCell *directions;
    UILabel *venueLabel;

    NSString *phoneText;
    NSString *addressText;
    NSString *venueText;
    NSString *beersavailable;

    NSString *latitudeText;
    NSString *longitudeText;
    }

@property (nonatomic, retain) IBOutlet UITableViewCell *phone;
@property (nonatomic, retain) IBOutlet UITableViewCell *address;
@property (nonatomic, retain) IBOutlet UITableViewCell *directions;
@property (nonatomic, retain) IBOutlet UILabel *venueLabel;

@property (nonatomic, retain) NSString *phoneText;
@property (nonatomic, retain) NSString *addressText;
@property (nonatomic, retain) NSString *venueText;
@property (nonatomic, retain) NSString *beersavailble;
@property (nonatomic, retain) NSString *latitudeText;
@property (nonatomic, retain) NSString *longitudeText;


@end

Lastly, finderdetail.m grabs these values, assigns them to the variables, and spits them into the table:

@implementation FinderDetail

@synthesize venueLabel, phone, address, directions;
@synthesize phoneText, addressText, venueText, beersavailble, latitudeText, longitudeText;
NSString *notlisted;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)setDetails:(NSString *)v phone:(NSString *)p address:(NSString *)a beersavailable:(NSString *)ba latitude:(NSString *)lat longitude:(NSString *)lon
{
    NSLog(@"venue: %@",v);
    NSLog(@"phone: %@",p);
    NSLog(@"address: %@",a);
    NSLog(@"beersavailable: %@",ba);
    NSLog(@"%@",lat);
    NSLog(@"%@",lon);

    latitudeText = lat;
    longitudeText = lon;  
    phoneText = p;
    addressText = a;
    venueText = v;
    beersavailble = ba;


    NSLog(@"%@", latitudeText);
    NSLog(@"%@", longitudeText);


    notlisted = @"Not Listed";
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"Latitude: %@", latitudeText);
    NSLog(@"Longitude: %@", longitudeText);


    phone.detailTextLabel.text = phoneText;
    address.detailTextLabel.text = addressText;
    self.venueLabel.text = venueText;

    if(phoneText == nil){
        phone.detailTextLabel.text = notlisted;
    }

    if(addressText == nil){
        address.detailTextLabel.text = notlisted;
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if(section ==0)
        return 1;
    else
    if(section ==1)
        return 1;
    else
    if(section ==2)
        return 1;
    else
    return 0;
}
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"%@",indexPath);

        if((indexPath.section==0) && (indexPath.row ==0))
        {
            NSLog(@"%@",phoneText);
        }

        if((indexPath.section==1) && (indexPath.row ==0))
        {
            NSLog(@"%@",addressText);
        }

        if((indexPath.section==2) && (indexPath.row ==0))
        {
            NSLog(@"%@",latitudeText);
            NSLog(@"%@",longitudeText);
        }
    }

The initial phoneText will display in an NSLog, but the addressText and latitudeText and longitudeText return null. I can put phoneText in one of those lower if statements and it too returns null. Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

垂暮老矣 2024-12-28 20:24:00

当您执行以下操作时,您实际上并没有使用您的@property

latitudeText = lat;
longitudeText = lon;  
phoneText = p;
addressText = a;
venueText = v;
beersavailble = ba;

此外,每次在初始时间之后执行这些分配时(当它们仍然nil)。

你真正想要的是:

self.latitudeText = lat;
self.longitudeText = lon;  
self.phoneText = p;
self.addressText = a;
self.venueText = v;
self.beersavailble = ba;

此外,还有一个 NSString (还有 NSDataNSSet 等)@property ,最好将它们定义为 copy,因为传入 NSMutableString 是完全有效的(因为它是 NSString 的子类) code>),然后可以从外部更改内容这个对象的:

@property (nonatomic, copy) NSString *phoneText;
@property (nonatomic, copy) NSString *addressText;
@property (nonatomic, copy) NSString *venueText;
@property (nonatomic, copy) NSString *beersavailble;
@property (nonatomic, copy) NSString *latitudeText;
@property (nonatomic, copy) NSString *longitudeText;

最后,您得到 NSLog 输出的 (NULL) 表明 ivars 被设置为 nil (并且很可能被释放),并且您正在使用ARC(自动引用计数),而不是手动保留/释放/自动释放。

You aren't actually using your @property when you are doing the following:

latitudeText = lat;
longitudeText = lon;  
phoneText = p;
addressText = a;
venueText = v;
beersavailble = ba;

Also, you are leaking memory every time those assignments are performed after the initial time (when they were still nil).

What you really want is:

self.latitudeText = lat;
self.longitudeText = lon;  
self.phoneText = p;
self.addressText = a;
self.venueText = v;
self.beersavailble = ba;

Also, with a NSString (also NSData, NSSet, etc.) @property, it is better to define them as a copy, since it would be perfectly valid to pass in a NSMutableString instead (since it is a subclass of NSString), which then the contents could be altered externally of this object:

@property (nonatomic, copy) NSString *phoneText;
@property (nonatomic, copy) NSString *addressText;
@property (nonatomic, copy) NSString *venueText;
@property (nonatomic, copy) NSString *beersavailble;
@property (nonatomic, copy) NSString *latitudeText;
@property (nonatomic, copy) NSString *longitudeText;

Finally, the fact that you get (NULL) outputted by NSLog suggests the ivars are getting set to nil (and most likely released), and you are using ARC (Automatic Reference Counting), instead of manual retain/release/autorelease.

寒冷纷飞旳雪 2024-12-28 20:24:00

setDetails 中,您需要使用属性来保留对象并释放以前的对象。直接分配给 ivars 会破坏属性设置器/获取器,并且它们提供的内存管理会丢失。基本上,如果定义了属性,则每次都会使用它们。

由于对象没有被保留,因此它们的内存可以被重用,并且可能会发生不可预测的结果,例如值变为 nil。

发现此类问题的一种方法是在模拟器运行中打开 NSZombies。即使我没有遇到问题,我偶尔也会这样做,只是作为检查。

要解决此问题,请将 setDetails 重写为:

-(void)setDetails:(NSString *)v phone:(NSString *)p address:(NSString *)a beersavailable:(NSString *)ba latitude:(NSString *)lat longitude:(NSString *)lon
{
    self.latitudeText = lat;
    self.longitudeText = lon;  
    self.phoneText = p;
    self.addressText = a;
    self.venueText = v;
    self.beersavailble = ba;

    self.notlisted = @"Not Listed";
}

确保不会无意中不使用属性的一种方法是使用与属性稍有不同的名称来定义 ivars。综合语句支持这一点。具体方法如下:
@interface中:

NSString *_latitudeText;
...
@property (nonatomic, retain) NSString *latitudeText;

@implementation

@synthesize latitudeText = _latitudeText;

In setDetails you need to use the properties in order to retain the objects and release previous objects. Assigning directly to the ivars subverts the properties setters/getters and the memory management they provide is lost. Basically if properties are defined use them every time.

Since the objects are not being retained their memory can be reused and unpredictable results can occur such as the values becoming nil.

One way to find such problems is to turn on NSZombies in the simulator runs. I do this occasionally even when I am not having problems just as a check.

To fix the problem rewrite setDetails as:

-(void)setDetails:(NSString *)v phone:(NSString *)p address:(NSString *)a beersavailable:(NSString *)ba latitude:(NSString *)lat longitude:(NSString *)lon
{
    self.latitudeText = lat;
    self.longitudeText = lon;  
    self.phoneText = p;
    self.addressText = a;
    self.venueText = v;
    self.beersavailble = ba;

    self.notlisted = @"Not Listed";
}

One way to insure that properties are not inadvertently not used is to define the ivars with a slightly different name than the properties. The synthesize statement supports this. Here is how:
in the @interface:

NSString *_latitudeText;
...
@property (nonatomic, retain) NSString *latitudeText;

in the @implementation

@synthesize latitudeText = _latitudeText;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文