如何将NSString从一个UIViewController传递到另一个UIViewController? [英] How to pass the NSString from One UIViewController to Another UIViewController?

查看:190
本文介绍了如何将NSString从一个UIViewController传递到另一个UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复:

iOS - 将变量传递给查看控制器

有两个ViewController(ViewController1和ViewController2),我想传递一个String值和一个IBAction从ViewController1到ViewController2。我试图读取ViewController2的ViewDidLoad方法中的NSString(* check)值,但它返回一个空值。

I have two Viewcontrollers (ViewController1 and ViewController2) , And i want to pass a String value along with a IBAction from ViewController1 to ViewController2. I am trying to read the NSString (*check) value in ViewController2's ViewDidLoad method, but it's returning a null value.

帮助我,并提前感谢。

Help me out and Thanks in advance.

// ViewController1

@interface ViewController1 : UIViewController {

        IBOutlet UIButton *btn1;
        IBOutlet UIButton *btn2;
        NSString *check;
    }
#import "ViewController.h"

-(IBAction)buttonClicked:(id)sender {

    check = [[NSString alloc] init];

    if (btn1.tag == 0) {
        check = @"foo";
        NSLog(@"%@",check);    
    }
    if (btn2.tag == 1) {
        check = @"bar";
        NSLog(@"%@",check);
    }
} 

b
$ b

in my second view controller ,

#import <UIKit/UIKit.h>

@class ViewController1;

@interface ViewController2 : UIViewController {

    ViewController1 *viewCon;
}

@property(nonatomic,retain) ViewController *viewCon;

//.m

#import "ViewController2.h"
#import "ViewController1.h"

@synthesize viewCon,

 - (void)viewDidLoad
{
    ViewController *myVC = [[ViewController alloc] init];
    NSLog(@"  Label  %@",myVC.check);
    [super viewDidLoad];
}


推荐答案

-(IBAction)buttonClicked:(id)sender {

   //check = [[NSString alloc] init]; -- NO Need

    if (btn1.tag == 0) {
        check = @"foo";
        NSLog(@"%@",check);    
    }
    if (btn2.tag == 1) {
        check = @"bar";
        NSLog(@"%@",check);
    }

  ViewController2 *obj = [[ViewController2 alloc] initWithNibName:@"ViewController2"];
  [obj setCheck:check];
  //push to ViewController2
  [obj release];

}

#import <UIKit/UIKit.h>

@interface ViewController2 : UIViewController {

        NSString *check;
}
@property (nonatomic, retain) NSString *check;


//.m

#import "ViewController2.h"

@synthesize check;


 - (void)viewDidLoad
{
    NSLog(@"  Label  %@",check);
}

这篇关于如何将NSString从一个UIViewController传递到另一个UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆