首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >存储协议/委派会产生正确的对象

存储协议/委派会产生正确的对象
EN

Stack Overflow用户
提问于 2013-12-09 13:18:50
回答 1查看 47关注 0票数 0

我已经使用在第一个视图控制器中分配的User对象创建了一个User类。而在第一个视图控制器中,一些对象属性填充了一些数据。我使用第二个视图控制器来获取额外的用户信息,然后将其发送回第一个视图控制器以存储在剩余的对象属性中。我正在使用一个协议和委托来完成这项工作。我按照这里的说明操作:Passing Data between View Controllers

看起来我做的一切都是正确的,除了我不知道如何将结果与我的对象的属性相关联。如果能够返回到调用第二个视图控制器的第一个视图控制器中的原始方法,那就太好了。这个是可能的吗?我看到的许多答案都需要全局变量,但我不确定这对我的情况是否必要。

代码语言:javascript
复制
/**Implementation of Main View Controller **/
-(IBAction)signUpButton
{

    User * firstUser = [[User alloc] init];

    firstUser.userName = userNameField.text;
    firstUser.password = passwordField.text;

    /**second view controller **/

    SetUp *setUpView = [[SetUp alloc] initWithNibName:Nil bundle:Nil];

    setUpView.delegate = self;

    [self presentViewController:setUpView animated:YES completion:^{ }];

    firstUser.zone = holdZoneInfo;
    firstUser.area= holdAreaInfo;

    NSLog(@"first User %@, %@, %@, %@",firstUser.userName, firstUser.password, firstUser.zone, firstUser.area);

    /**username and password display fine, but zone and area are null since the delegation operation hasn't been completed as this point **/

}

/**Protocol Method Declared in Main View Controller**/
-(void) sendUserInfoBack: (SetUp *) SetUpController didFinishWithZone:(NSString*)item1 didFinishWithArea:(NSString*) item2
{

    holdZoneInfo = item1;
    holdAreaInfo = item2;

    NSLog(@"Delegation result: %@ %@", holdZoneInfo, holdAreaInfo);
    /**This displays correctly**/

}

/**Second view controller implementation file**/

-(IBAction)goToMainView:(id)sender
{
    NSString * neededZoneStore = zoneField.text;
    NSString * neededAreaStore = areaField.text;

    User * user = [[User alloc] init];

    user.zone = neededZoneStore;
    user.area = neededAreaStore;

    [self.delegate sendUserInfoBack:self didFinishWithZone: neededZoneStore didFinishWithArea: neededAreaStore];


    [self dismissViewControllerAnimated:YES completion:NULL];
}

所以我需要firstUser.zone = holdZoneInfo,但我无法实现

EN

回答 1

Stack Overflow用户

发布于 2013-12-09 13:30:38

SetUp视图控制器类中,声明类型为Useruser属性。在alloc/init SetUp视图控制器时,将user属性设置为firstUser。当SetUp视图控制器调用委托方法时,返回firstUser对象。

并让SetUp视图控制器用来自用户的数据填充User对象。

代码语言:javascript
复制
// in signUpButton
setUpView.user = firstUser;

重构委托方法的名称:

代码语言:javascript
复制
- (void)setUpViewController:(SetUp *)controller exitedWithUser:(User *)user;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20463676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档