我知道这个问题已经被问了一遍又一遍,但对我来说仍然很模糊,所以我想用我的代码做一个例子可能会更容易。
我知道你可以使用:
假设我的第一个视图控制器标头中有这段代码:
#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];
}
我该怎么打电话evTe在我的 SecondViewController 中? (也许使用代表?)。
这是我在第二个视图控制器 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);
}
多谢 !
为托布编辑
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
- (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 得到任何日志..