iOS - 传递变量以查看控制器 [英] iOS - Passing variable to view controller

查看:76
本文介绍了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 ,合成变量:

// 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天全站免登陆