iOS – 将variables传递给视图控制器

我有一个视图控制器的视图,当我在屏幕上显示这个视图,我想能够从调用类传递variables,以便我可以设置标签的值等。

首先,我试着为其中一个标签创build一个属性,然后从调用类中调用它。 例如:

SetTeamsViewController *vc = [[SetTeamsViewController alloc] init]; vc.myLabel.text = self.teamCount; [self presentModalViewController:vc animated:YES]; [vc release]; 

但是,这不起作用。 所以我尝试创build一个方便的初始化。

 SetTeamsViewController *vc = [[SetTeamsViewController alloc] initWithTeamCount:self.teamCount]; 

然后在SetTeamsViewController

 - (id)initWithTeamCount:(int)teamCount { self = [super initWithNibName:nil bundle:nil]; if (self) { // Custom initialization self.teamCountLabel.text = [NSString stringWithFormat:@"%d",teamCount]; } return self; } 

然而,这也没有工作。 它只是加载我给nib文件中的标签的任何值。 我已经用NSLog()散布代码,它正在传递正确的variables值,它只是不设置标签。

任何帮助将不胜感激。

编辑:我刚刚尝试设置实例variables在我指定的初始值设定项,然后在viewDidLoad中设置标签,并工作! 这是做这个最好的方法吗?

另外,当closures这个模式视图控制器时,我也在调用ViewController的视图中更新一个button的文本。 但是,如果我再次按下该button(再次显示模式视图),而另一个视图在屏幕上显示animation,则该button暂时将其原始值(来自笔尖)。 有人知道为什么吗?

在初始化视图控制器的时候,在initWithNibName方法中,驻留在视图控制器中的视图还没有初始化,你还不能设置它们的属性。 做任何你需要的是基于“viewDidLoad”方法的视图。

我不是专业人士,但这可能会帮助你。

在标题view1.h ,声明所需的属性:

 // view1.h @interface view1 : UIViewController { NSString *passingVariable; } @property (nonatomic, strong) NSString *passingVariable; @end 

然后在view1的实现中,综合variables:

 // view1.m @implementation view1 @synthesize passingVariable; // the rest of the implementation @end 

最后在其他视图控制器的实现中,view2:

 // view2.m #import "view1.h" @implementation view2 -(IBAction)changeview { view1 *myview = [[view1 alloc] init]; myview.passingVariable = @"Hello Variable"; [self.navigationController pushViewController:myview animated:YES]; } @end 

这里我试图从view2移动到view1并初始化view1的passingVariable ivar。 希望这会帮助你。

在这里,我将ViewController的标签文本传递给SecondViewController的标签文本

ViewController.h

 #import <UIKit/UIKit.h> @interface ViewController : UIViewController { // please make your control on XIB set these IBOutlet's //I'm not showing how to connect these with XIB IBOutlet UILabel *lblView; IBOutlet UIButton *buttonGo; } //this is method which will push the view -(IBAction)buttonGoClickAction:(id)sender; 

ViewController.m

 -(IBAction)buttonGoClickAction:(id)sender { SecondViewController *secondViewObject = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; //before pushing give the text secondViewObject.string = lblView.text; [self.navigationController pushViewController:secondViewObject animated:YES]; } 

SecondViewController.h

 #import <UIKit/UIKit.h> @interface SecondViewController : UIViewController { IBOutlet UILabel *labelView; NSString *string; } //set the string property @property(nonatomic, retain) NSString *string; @end 

SecondViewController.m

  #import "SecondViewController.h" @implementation SecondViewController //synthesize string here @synthesize string; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. //Here you will get the string labelView.text = string; } 

首先,你检查你是否附加这个标签IBOutlet在XIB或不,如果你通过接口生成器….

像这样使用它….

 SetTeamsViewController *vc = [[SetTeamsViewController alloc] initWithTeamCount:teamCount]; 

在.h文件中取一个stringvariables,并在这里设置该string。NSSting * str in .h

 - (id)initWithTeamCount:(int)teamCount { self = [super init]; if (self) { // Custom initialization str = [NSString stringWithFormat:@"%d",teamCount]; } return self; } 

并在viewDidLoad:viewWillApear中设置你的标签

  self.teamCountLabel.text = str; 

愿这能帮助你

正如stavash所说,xib中的控件是在视图中创build的。 更准确地说,它们是用这一行创build的:

  [super viewDidLoad]; 

所以,mylabel在那个时间之前是不存在的(这是零)。

最简单的方法就是这样做:

  SetTeamsViewController *vc = [[SetTeamsViewController alloc] init]; [self presentModalViewController:vc animated:YES]; vc.myLabel.text = self.teamCount; [vc release]; 

更长,但更正确的path是在SetTeamsViewController类中有成员NSString *,在显示窗口之前将其设置为teamCount,并在视图中加载将该元素值放入标签。

CDT

这取决于你的需要。 你可以使用Singleton类来在不同的类之间共享你的variables。 定义你想在你的DataClass中共享的所有variables。

在.h文件(其中RootViewController是我的DataClass,用你的新类replace名称)

 +(RootViewController*)sharedFirstViewController; 

在.m文件中

 //make the class singleton:- +(RootViewController*)sharedFirstViewController { @synchronized([RootViewController class]) { if (!_sharedFirstViewController) [[self alloc] init]; return _sharedFirstViewController; } return nil; } +(id)alloc { @synchronized([RootViewController class]) { NSAssert(_sharedFirstViewController == nil, @"Attempted to allocate a second instance of a singleton."); _sharedFirstViewController = [super alloc]; return _sharedFirstViewController; } return nil; } -(id)init { self = [super init]; if (self != nil) { // initialize stuff here } return self; } 

之后你可以在任何其他类中使用你的variables

 [RootViewController sharedFirstViewController].variable 

希望这是帮助你:)

在Storyboard中,正确的方法是在performSegueWithIdentifier中将indexPath作为发送者parameter passing

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"segueIdentifier" sender:indexPath]; } 

并在目标控制器中设置一个属性:

 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString: @"segueIdentifier"]) { NSIndexPath *indexPath = sender; DetailViewController *dest = [segue destinationViewController]; dest.usersArray = [self.usersArray objectAtIndex:indexPath.row]; } } 

当我需要另一个类来获得上一个类的variables时,我所做的事情是,我设置了一个全局类,它将存储更多位置的值,或者在可以设置@publicvariables的接口中。 这些variables可以使用您为下一个视图创build的控制器来设置。

controller-> pub_var1 = val1; controller-> pub_var2 = val2;

在将视图传递到根控制器之前或在您调用下一个视图之前,将完成此操作。 你将需要#import“class.h”,以便你可以访问这些公共variables。

如果不清楚,我可以显示代码