变量在didSelectRowAtIndexPath上清空或擦除

时间:2011-12-19 00:00:27

标签: iphone xcode variables tableview

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

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。感谢!!!

2 个答案:

答案 0 :(得分:2)

当您执行以下操作时,您实际上并未使用@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的子类),然后内容可以在此对象的外部进行更改,这将完全有效:

@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;

最后,(NULL)输出NSLog的事实表明ivars已设置为nil(并且很可能已发布),并且您正在使用ARC(自动引用计数< / em>),而不是手动保留/释放/自动释放。

答案 1 :(得分:1)

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

由于没有保留对象,因此可以重用其内存,并且可能会出现不可预测的结果,例如值变为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。 synthesize语句支持这一点。方法如下: 在@interface

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

中的

@synthesize latitudeText = _latitudeText;