问题描述
我知道这个问题是一遍又一遍地问到的,但是对我来说还是很模糊的,所以我想用我的代码编写示例可能会更容易.
I know this question as been asked over and over but it's still quite obscure to me, so I guess making an example with my code instead will probably be easier .
我知道您可以使用:
- 一个全局变量,(不是一个好习惯).
- 使用委托人
- 使用单例
说我的第一个视图控制器标题中有这段代码:
Say I've got this piece of code here in my first view controller header :
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
IBOutlet UITextField *Te;
NSInteger evTe;
}
@property (nonatomic, retain) UITextField *Te;
@property (nonatomic) NSInteger evTe;
- (IBAction) makeKeyboardGoAway;
@end
然后在我的实现文件中
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize Te;
- (IBAction) makeKeyboardGoAway;
{
[Te resignFirstResponder];
evTe = [Te.text intValue];
}
如何在SecondViewController中调用 evTe ? (也许使用委托人?).
How would I call evTe in my SecondViewController ? ( maybe using a delegate ?) .
这是我在第二个视图控制器头中得到的内容:
This is what I've got in the second view Controller, header :
@interface SecondViewController : UIViewController {
NSInteger evTe;
}
@property (nonatomic) NSInteger evTe;
和实现:
- (IBAction) makeKeyboardGoAway;
{
FirstViewController *first = [[FirstViewController alloc] init];
first.evTe = self.evTe;
NSLog(@"second value is %i",evTe);
}
非常感谢!
编辑Tob
FirstViewController.m
FirstViewController.m
- (IBAction) makeKeyboardGoAway;
{
evTe = [Te.text intValue];
NSLog(@"The value of integer num is %i", evTe);
NSDictionary *changedValues = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:evTe] forKey:@"evTe"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"evTeChanged" object:self userInfo:changedValues];
}
SecondViewController.m
SecondViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToCall:) name:@"evTeChanged" object:nil];
}
- (void)methodToCall:(NSNotification *)aNotification{
NSDictionary *changedValues = [[aNotification userInfo] objectForKey:@"evTe"];
NSString *dictionaryString = [changedValues description];
NSLog(@"Notification returning %d",dictionaryString);
}
不幸的是,我没有从SecondView中获取任何日志..
Unfortunately I'm not getting any log from the SecondView ..
推荐答案
通过在两个视图控制器中创建一个称为evTe的@property或其他属性.
By creating a @property called evTe or whatever in both view controllers.
如果FirstViewController负责创建SecondViewController,则可以将evTe的值存储在FirstViewController的属性中,然后在创建SecondViewController之后,也要在其中设置evTe属性.
If the FirstViewController is responsible for creating the SecondViewController you could store the value of evTe in a property in FirstViewController and then after you have created the SecondViewController you set the evTe property there as well.
- (IBAction) makeKeyboardGoAway;
{
[Te resignFirstResponder];
self.evTe = [Te.text integerValue];
}
//创建SecondViewController的其他方法
// other method where SecondViewController is created
SecondViewController* second = [[SecondViewController alloc] init];
second.evTe = self.evTe;
// do what ever
-编辑-
@interface FirstViewController : UIViewController {
IBOutlet UITextField *Te;
NSInteger evTe;
}
@property (nonatomic, retain) UITextField *Te;
@property (nonatomic) NSInteger evTe;
这篇关于将变量从一个视图传递到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!