我正在为 iPhone 开发一个基于标签栏的应用程序。流程如下:当应用程序运行时,我抛出带有登录表单的模态视图:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
tabBarController.delegate = self;
// Add the tab bar controller's view to the window and display.
self.window.rootViewController = self.tabBarController;
[self addTabBarArrow];
LoginViewController *loginViewController = [[LoginViewController alloc] init];;
[window addSubview:tabBarController.view];
[self.tabBarController presentModalViewController:loginViewController animated:YES];
[window makeKeyAndVisible];
return YES; }
在模式视图 LoginViewController.h (child) 的登录中,我实现了一个协议:
@protocol PassUserInfoDelegate <NSObject>
@required
- (void) passUserInfo: (NSString *)string;
@end
当用户填写表单时,我创建一个 NSURLConnection,并在 connectionDidFinishLoading 方法中从 JSON 请求中获取用户值:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *respuestaServidor = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSDictionary *dictionary = [respuestaServidor JSONValue];
idJson = [dictionary objectForKey:@"id"];
NSString *user_loginJson = [dictionary objectForKey:@"user_login"];
if ([idJson isEqualToString:@"null"] && [user_loginJson isEqualToString:@"null"]) {
NSLog(@"Login incorrecto");
} else {
NSLog(@"Procedo a loguear usuario");
}
[indicator stopAnimating];
[indicator release];
}
在 HomeViewController.h (父级)中,我得到了委托:
@interface HomeViewController : UIViewController <PassUserInfoDelegate> {
LoginViewController *protocolTest;
IBOutlet UILabel *nombreUsuario;
NSString *usuario;
}
@property (nonatomic, retain) IBOutlet UILabel *nombreUsuario;
@property (copy) NSString *usuario;
- (void) passUserInfo:(NSString *)string;
@end
在 HomeViewController.m 中,我实现了 Protocol 方法:
- (void) passUserInfo:(NSString *)jSonString
{
userName.text = [[NSString alloc] initWithFormat:@"Welcome %@", jSonString];
}
在 viewDidAppear 方法中,我调用 LoginViewController 类中实现的 loginSuccess 方法
-(void) viewDidAppear:(BOOL)animated{
protocolTest = [[LoginViewController alloc] init];
[protocolTest setDelegate:self];
[protocolTest loginSuccess];
}
LoginViewController 类中实现的 loginSuccess 方法:
- (void)loginSuccess
{
[[self delegate] passUserInfo:idJson];
}
它应该将 idJson 值传递给 HomeViewController(父级)。问题是当我关闭模态视图表单时,idJson 值为“NIL”,所以在 HomeViewController 中我无法获取此值。如果我这样做:
[[self delegate] passUserInfo:@"hello"];
我在 HomeViewController(父级)中得到了 hello 字符串我做错了什么?
提前致谢!!!