我正在研究這個項目,我有一個用戶類,我的主視圖控制器和我的SetUp控制查看器(第二個控制查看器)。 User類具有所有的User屬性,主視圖控制器創建用戶名/密碼,然後調用SetUp視圖控制器,SetUp視圖控制器允許用戶輸入其他信息。我在SetUp視圖控制器中創建了一個協議,在用戶在第二個視圖控制器中輸入附加信息後,在我的主視圖控制器中調用委託方法。委託方法返回一個用戶對象,但這是我迷路的地方。 User對象在哪裏被返回?它絕對不會去我想要的地方。如何使用委託和協議返回對象
/**Main View Controller Implementation**/
#import "BWViewController.h"
#import "User.h"
@interface BWViewController()
@end
@implementation BWViewController
@synthesize holdPassword,holdUserName,holdZone,holdArea;
-(IBAction)signUpButton
{
firstUser = [[User alloc] init];
firstUser.userName = userNameField.text;
firstUser.password = passwordField.text;
SetUp *setUpView = [[SetUp alloc] initWithNibName:Nil bundle:Nil];
setUpView.delegate = self;
User * bufferUser;
bufferUser = [self presentViewController:setUpView animated:YES completion:^{ }]; /**This is where I want the User object from the delegate method return to**/
}
/**Delegate method in Main view controller**/
-(User*) sendUserInfoBack: (SetUp *) SetUpController didFinishWithZoneInfo:(NSString*)zoneInfo didFinishWithAreaInfo:(NSString*) areaInfo
{
User*holdUser;
holdZone = zoneInfo;
holdArea = areaInfo;
holdUser.userZone = holdZone;
holdUser.userArea = holdArea;
return holdUser; /**return this to bufferUser object in method above**/
}
/**Second View controller Interface**/
#Import "User.h"
@class SetUp;
@protocol SetUpControllerDelegate <NSObject>
-(User*) sendUserInfoBack: (SetUp *) SetUpController didFinishWithZoneInfo:(NSString*)zoneInfo didFinishWithAreaInfo:(NSString*) areaInfo;
@property (weak,nonatomic) id<SetUpControllerDelegate>delegate;
@end
/**Second View controller implementation**/
-(IBAction)goToMainView:(id)sender
{
NSString * neededZoneStore = Zone.text;
NSString * areaStore = Area.text;
User * user = [[User alloc] init];
user.userZone = neededZoneStore;
user.userArea = neededAreaStore;
[self.delegate sendUserInfoBack:self didFinishWithZoneInfo:neededZoneStore didFinishWithAreaInfo:neededAreaStore];
[self dismissViewControllerAnimated:YES completion:NULL];
}
你能否儘可能簡單地澄清你想要做的事情? – nhgrif
'@protocol SetUpControllerDelegate' 當然。我試圖返回一個用戶對象與設置視圖控制器中獲得的變量到我的主視圖控制器中的bufferUser對象。 –
Brosef