【问题标题】:How to pass the NSString from One UIViewController to Another UIViewController? [duplicate]如何将 NSString 从一个 UIViewController 传递到另一个 UIViewController? [复制]
【发布时间】:2012-09-08 08:10:38
【问题描述】:

可能重复:
iOS - Passing variable to view controller

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

帮帮我,提前谢谢。

// 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);
    }
} 

在我的第二个视图控制器中,

#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];
}

【问题讨论】:

标签: iphone objective-c ios ipad cocoa


【解决方案1】:
-(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);
}

【讨论】:

    【解决方案2】:

    使用 ViewController2 的实例后跟点运算符将数据从 ViewController1 传递到 ViewController2:

    在 ViewController2 中创建一个 NSString *strCheck 的属性,因为你想将数据传递给 ViewController2。

    现在在 ViewController1 的按钮点击事件中

    -(IBAction)buttonClicked:(id)sender {
    {
    
     .......
     .......
    
     ViewController2 *objViewController2 = [ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
     objViewController2.strCheck = check; //ViewController1's value to ViewController2
    
    }
    

    【讨论】:

      【解决方案3】:

      查看此链接 -
      iOS - Passing variable to view controller
      我已经给出了同样的答案。

      您需要设置变量的@property@synthesized。更多内容请查看链接。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 2011-02-08
        • 2019-05-01
        • 1970-01-01
        相关资源
        最近更新 更多