我是iPhone編程的新手。我試圖從一個視圖控制器發送消息到另一個。這個想法是,viewControllerA從用戶獲取信息並將其發送到viewControllerB。 viewControllerB然後應該顯示在標籤中的信息。在類Obj-C之間發送消息?
viewControllerA.h
#import <UIKit/UIKit.h>
@interface viewControllerA : UIViewController
{
int num;
}
-(IBAction)do;
@end
viewControllerA.m
#import "viewControllerA.h"
#import "viewControllerB.h"
@implementation viewControllerA
- (IBAction)do {
//initializing int for example
num = 2;
viewControllerB *viewB = [[viewControllerB alloc] init];
[viewB display:num];
[viewB release];
//viewA is presented as a ModalViewController, so it dismisses itself to return to the
//original view, i know it is not efficient but it is not the problem with my code
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[super dealloc];
}
@end
viewControllerB.h
#import <UIKit/UIKit.h>
@interface viewControllerB : UIViewController
{
IBOutlet UILabel *label;
}
- (void)display:(int)myNum;
@end
viewControllerB.m
#import "viewControllerB.h"
#import "viewControllerA.h"
@implementation viewControllerB
- (void)display:(int)myNum {
NSLog(@"YES");
[label setText:[NSString stringWithFormat:@"%d", myNum]];
}
@end
YES已成功記錄,但標籤的文本不會更改。可以發送消息不是 訪問實例變量什麼的?
謝謝。
如果您沒有正確連接IBOutlet,則無論您在代碼中做什麼,都不會看到任何內容,因此請確保這些內容正確無誤。 – jakev 2010-12-16 07:18:41
檢查答案,如果你的問題已經解決,然後接受一個答案。 – Ishu 2010-12-16 08:10:47