Objective c 将协议/委派结果存储在正确的对象中
我创建了一个用户类,在第一个视图控制器中分配了一个用户对象。在第一个视图控制器中,一些对象属性填充了一些数据。我使用第二个视图控制器来获取额外的用户信息,然后将其发送回第一个视图控制器以存储在剩余的对象属性中。我用协议和授权来完成这件事。我按照这里的指示: 看起来我做的一切都是正确的,只是我不知道如何将结果与对象的属性关联起来。如果能够返回到调用第二个视图控制器的第一个视图控制器中的原始方法,那就太好了。这可能吗?我看到的很多答案都需要全局变量,但我不确定这对我的案例是否必要Objective c 将协议/委派结果存储在正确的对象中,objective-c,ios7,Objective C,Ios7,我创建了一个用户类,在第一个视图控制器中分配了一个用户对象。在第一个视图控制器中,一些对象属性填充了一些数据。我使用第二个视图控制器来获取额外的用户信息,然后将其发送回第一个视图控制器以存储在剩余的对象属性中。我用协议和授权来完成这件事。我按照这里的指示: 看起来我做的一切都是正确的,只是我不知道如何将结果与对象的属性关联起来。如果能够返回到调用第二个视图控制器的第一个视图控制器中的原始方法,那就太好了。这可能吗?我看到的很多答案都需要全局变量,但我不确定这对我的案例是否必要 /**Imple
/**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,但我无法实现这一点在
设置
视图控制器类中,声明类型为用户
的用户
属性。当您alloc
/init
aSetUp
视图控制器将user
属性设置为firstUser
。当SetUp
视图控制器调用委托方法时,返回firstUser
对象
并让设置
视图控制器使用来自用户的数据填充用户
对象
// in signUpButton
setUpView.user = firstUser;
重构委托方法的名称:
- (void)setUpViewController:(SetUp *)controller exitedWithUser:(User *)user;
谢谢你的建议,但我不确定我是否听从了。因此,我在设置视图控制器中创建了一个名为User的用户类属性。我可以alloc/init user并将所需的属性存储在我的设置视图控制器中,但我不知道如何将其与主视图控制器中的object firstUser连接起来。我想我开始看到了。因此,我需要将我的委托方法从:
-(void)sendUserInfoBack:(SetUp*)SetUpController didFinishWithZone:(NSString*)item1 didFinishWithArea:(NSString*)item2
更改为-(void)sendUserInfoBack:(SetUp*)SetUpController退出用户:(User*)代码>正确吗?不应该是-(用户*)发送用户信息返回:(设置*)设置控制器退出用户:(用户*)用户代码>委托方法的返回值仅用于委托对象,因此在此处返回void。委派对象已具有对用户的访问权限。注意:在这种情况下,设置视图控制器被视为委派对象。主视图控制器是代理对象。对不起,条件不好,好像不行。我认为我在主视图控制器实现文件中未正确定义此方法:-(void)sendUserInfoBack:(SetUp*)SetUpController exitedWithUser:(User*)User代码>现在我只是把它定义为返回用户代码>