didSelectRowAtIndexPath 上的变量清空或擦除
由于某种原因,我无法在以下代码中的第一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您执行以下操作时,您实际上并没有使用您的
@property
:此外,每次在初始时间之后执行这些分配时(当它们仍然
nil)。
你真正想要的是:
此外,还有一个
NSString
(还有NSData
、NSSet
等)@property
,最好将它们定义为copy
,因为传入NSMutableString
是完全有效的(因为它是NSString
的子类) code>),然后可以从外部更改内容这个对象的:最后,您得到
NSLog
输出的(NULL)
表明 ivars 被设置为 nil (并且很可能被释放),并且您正在使用ARC(自动引用计数),而不是手动保留/释放/自动释放。You aren't actually using your
@property
when you are doing the following: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:
Also, with a
NSString
(alsoNSData
,NSSet
, etc.)@property
, it is better to define them as acopy
, since it would be perfectly valid to pass in aNSMutableString
instead (since it is a subclass ofNSString
), which then the contents could be altered externally of this object:Finally, the fact that you get
(NULL)
outputted byNSLog
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.在
setDetails
中,您需要使用属性来保留对象并释放以前的对象。直接分配给 ivars 会破坏属性设置器/获取器,并且它们提供的内存管理会丢失。基本上,如果定义了属性,则每次都会使用它们。由于对象没有被保留,因此它们的内存可以被重用,并且可能会发生不可预测的结果,例如值变为 nil。
发现此类问题的一种方法是在模拟器运行中打开 NSZombies。即使我没有遇到问题,我偶尔也会这样做,只是作为检查。
要解决此问题,请将
setDetails
重写为:确保不会无意中不使用属性的一种方法是使用与属性稍有不同的名称来定义 ivars。综合语句支持这一点。具体方法如下:
在
@interface
中:在
@implementation
中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: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
:in the
@implementation