我试图了解课程是如何运作的,而且我已经成功了。该字符串将保存到viewcontroller A中的place类,但它在viewconrollerB中访问null。有人可以解释为什么会这样吗?就像我说的那样,我对课程非常不熟悉并试图学习。
来自视图控制器A的代码(当您单击按钮时,将保存与该按钮关联的名称)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
self.place=[[Place alloc]init];
return self;
}
- (void) buttonPressed:(UIButton *) sender
{
NSLog(@"works");
self.place.name=self.name;
NSLog(@"%@",self.place.name);
}
The Class.m
@implementation Place
-(id)init {
self=[super init];
return self;
}
@end
viewController B:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
self.title=@"Shack";
}
self.place=[[Place alloc]init];
return self;
}
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"%@",self.place.name);
}
Class.h
@interface Place : NSObject
@property (strong,nonatomic) NSString *name;
@end
Viewcontrollerb.h
#import "Place.h"
@interface FavoritesTableViewController : UITableViewController
@property (nonatomic) NSString * name;
@property (strong,nonatomic) Place * place;
@end
ViewcontrollerA.h
@interface MoreViewController : UIViewController
@property (nonatomic,strong) NSString * name;
@property (nonatomic,strong) Place *place;
@end
答案 0 :(得分:0)
在ViewControllerB中,您可以在initwithstyle中创建一个新的Place。此位置与ViewControllerA中创建的位置不同。因此,当您访问name属性时,您访问null。要解决此问题,您需要将第二个vc上的位置设置为第一个vc上的位置。这将创建一个指针,允许您访问原始名称。