gpt4 book ai didi

iphone - didSelectRowAtIndexPath 上的变量清空或删除

转载 作者:行者123 更新时间:2023-12-03 20:53:07 25 4
gpt4 key购买 nike

出于某种原因,我无法在以下代码中的第一个 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。谢谢!!!

最佳答案

当您执行以下操作时,您实际上并未使用您的@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;

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

关于iphone - didSelectRowAtIndexPath 上的变量清空或删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8555846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com