如何初始化另一个视图控制器的值

时间:2013-12-24 07:55:50

标签: ios iphone ios7

在我的应用中,当用户从浏览器重定向到应用时,我正在呈现注册页面。我希望从url变量中插入值。所以首先我从url获取值,然后用文本框值显示该页面。但它没有设置文本框值。

我的代码:

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    NSString *urlString=[NSString stringWithContentsOfURL:url encoding:nil error:nil];
 NSRange r = [urlString rangeOfString:@"gratzeez://" options:NSCaseInsensitiveSearch];

    if(r.location != NSNotFound){
        NSLog(@"founddddd");
        NSLog(@"gratzeez url %@",url);
        //NSString *str=[url valueForKey:@"email"];
        NSArray *query = [[url query] componentsSeparatedByString:@"&"];
        NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:[query count]];
        for(NSString *parameter in query)
        {
            NSArray *kv = [parameter componentsSeparatedByString:@"="];
            [parameters setObject:[kv count] > 1 ? [[kv objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding] : [NSNull null]
                           forKey:[[kv objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];
        }

        NSLog(@"Parameters: %@", parameters);


        [self.tabBar dismissViewControllerAnimated:NO completion:nil];
        RegistrationViewController *RegistrationVC=[[RegistrationViewController alloc] initWithNibName:@"RegistrationViewController" bundle:nil];
        RegistrationVC.txtEmail.userInteractionEnabled=NO;
        NSLog(@"q = %@", [parameters objectForKey:@"email"]);

        UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:RegistrationVC];
        RegistrationVC.txtEmail.text=[parameters objectForKey:@"email"];
        NSLog(@"value%@",RegistrationVC.txtEmail.text); //here getting null value
        [self.tabBar presentViewController:navController animated:NO completion:nil];
        navController.navigationItem.leftBarButtonItem=nil;
        return YES;

    }

我通过这种方式呈现了registrationviewcontroller。

RegistrationViewController *RegistrationVC=[[RegistrationViewController alloc] init ];

    NSLog(@"q = %@", [parameters objectForKey:@"email"]);
    UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:RegistrationVC];

    NSLog(@"txtemail %@ value%@",RegistrationVC.txtEmail,RegistrationVC.txtEmail.text);
    [self.tabBar presentViewController:navController animated:NO completion:^{
        RegistrationVC.txtEmail.userInteractionEnabled=NO;
        RegistrationVC.txtEmail.text=[parameters objectForKey:@"email"];
    }];

现在再次为两者提供空值。

1 个答案:

答案 0 :(得分:1)

正如我们在评论中看到的那样,当您尝试为其分配值时,不会创建txtEmail。

我建议你在方法presentViewController:animated:completion:中添加一个完成块。此时,您的控制器RegistrationVC将被加载并创建其视图(和子视图),因此您可以为元素分配值:

[self.tabBar presentViewController:navController animated:NO completion:^{
    RegistrationVC.txtEmail.text=[parameters objectForKey:@"email"];
}];