iOS - 将变量传递给视图控制器 [英] iOS - Passing variable to view controller

查看:14
本文介绍了iOS - 将变量传递给视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有视图控制器的视图,当我在屏幕上显示这个视图时,我希望能够从调用类向它传递变量,以便我可以设置标签等的值.

I have a view with a view controller and when I show this view on screen, I want to be able to pass variables to it from the calling class, so that I can set the values of labels etc.

首先,我只是尝试为其中一个标签创建一个属性,并从调用类调用它.例如:

First, I just tried creating a property for one of the labels, and calling that from the calling class. For example:

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

然而,这没有用.所以我尝试创建一个方便的初始化程序.

However, this didn't work. So I tried creating a convenience initializer.

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() 散布了代码,它传递了正确的变量值,只是没有设置标签.

However, this didn't work either. It's just loading whatever value I've given the label in the nib file. I've littered the code with NSLog()s and it is passing the correct variable values around, it's just not setting the label.

任何帮助将不胜感激.

我刚刚尝试在我指定的初始值设定项中设置一个实例变量,然后在 viewDidLoad 中设置标签,并且有效!这是最好的方法吗?

I've just tried setting an instance variable in my designated initializer, and then setting the label in viewDidLoad and that works! Is this the best way to do this?

此外,当关闭这个模态视图控制器时,我也在调用 ViewController 的视图中更新按钮的文本.但是,如果我再次按下此按钮(再次显示模态视图),而另一个视图正在屏幕上显示动画,则该按钮会暂时再次具有其原始值(来自笔尖).有谁知道这是为什么?

Also, when dismissing this modal view controller, I update the text of a button in the view of the calling ViewController too. However, if I press this button again (to show the modal view again) whilst the other view is animating on screen, the button temporarily has it's original value again (from the nib). Does anyone know why this is?

推荐答案

我不是专业人士,但这可能对您有所帮助.

I am not a pro but this may help you.

在头文件 view1.h 中,声明所需的属性:

In the header view1.h, declare the desired property:

// view1.h

@interface view1 : UIViewController {
    NSString *passingVariable;
}

@property (nonatomic, strong) NSString *passingVariable;

@end

然后在view1的实现中,合成变量:

and then in the implementation of view1, synthesize the variable:

// view1.m

@implementation view1

@synthesize passingVariable;

// the rest of the implementation

@end

最后在另一个视图控制器 view2 的实现中:

and, finally in the implementation of the other view controller, 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 移动到视图 1 并初始化 view1 的passingVariable ivar.希望这对你有帮助.

here i am trying to move from view2 to view 1 and also initializing the passingVariable ivar of view1. hope this will help you.

这篇关于iOS - 将变量传递给视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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