我有两点不明白:
第一,我在storyboard里加入一个viewcontroller视图控制器,然后创建一个viewcontroller的subclass,再将该视图的custom class设置成这个类了。那这个类的实例是怎么跟视图作用的?我可不可以创建多个实例,这样会不会就乱了?第二,看到别人的一个例子里 ,两个视图控制器之间传值,在viewcontrollerA.m中实例化了一个viewcontrollerB 之后在视图显示之前又释放了 这样可以么?为什么?代码如下:
第一个视图中
- (IBAction)passValueButton:(id)sender {
//构建UserEntity对象
UserEntity *userEntity = [[UserEntity alloc] init];
userEntity.userName = self.userNameTextFiled.text;
userEntity.gendar = self.gendarTextField.text;
userEntity.age = [self.ageTextField.text intValue];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
//设置SecondViewController中的值
secondView.userEntity = userEntity;
//跳转界面
[self presentModalViewController:secondView animated:YES];
[userEntity release];
[secondView release];
} 在第二个界面显示传过来值的方法:
[cpp] view plaincopy
- (void)viewDidLoad
{
[super viewDidLoad];
//显示从前一个界面传过来的值
self.userNameTextField.text = self.userEntity.userName;
self.gendarTextField.text = self.userEntity.gendar;
//NSString转换为int型
self.ageTextField.text = [NSString stringWithFormat:@"%d",self.userEntity.age];
}
第一,我在storyboard里加入一个viewcontroller视图控制器,然后创建一个viewcontroller的subclass,再将该视图的custom class设置成这个类了。那这个类的实例是怎么跟视图作用的?我可不可以创建多个实例,这样会不会就乱了?第二,看到别人的一个例子里 ,两个视图控制器之间传值,在viewcontrollerA.m中实例化了一个viewcontrollerB 之后在视图显示之前又释放了 这样可以么?为什么?代码如下:
第一个视图中
- (IBAction)passValueButton:(id)sender {
//构建UserEntity对象
UserEntity *userEntity = [[UserEntity alloc] init];
userEntity.userName = self.userNameTextFiled.text;
userEntity.gendar = self.gendarTextField.text;
userEntity.age = [self.ageTextField.text intValue];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
//设置SecondViewController中的值
secondView.userEntity = userEntity;
//跳转界面
[self presentModalViewController:secondView animated:YES];
[userEntity release];
[secondView release];
} 在第二个界面显示传过来值的方法:
[cpp] view plaincopy
- (void)viewDidLoad
{
[super viewDidLoad];
//显示从前一个界面传过来的值
self.userNameTextField.text = self.userEntity.userName;
self.gendarTextField.text = self.userEntity.gendar;
//NSString转换为int型
self.ageTextField.text = [NSString stringWithFormat:@"%d",self.userEntity.age];
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货