1
我有2個viewcontrollers,vc1和vc2。Xcode Objective C - 從另一個類更改UILabel文本
vc1有一個UILabel。 vc2有一個UIButton。當vc2上的按鈕被點擊時,我想讓vc1的uiLabel上的文字發生變化。我怎麼做?
vc1.h
@interface vc1 : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *textLabel;
@end
vc1.m
@interface vc1()
@end
@implementation vc1
@synthesize textLabel = _textLabel;
@end
vc2.m
#import "vc2.h"
#import "vc1.h"
@interface vc2()
@end
@implementation vc2
- (IBAction)onClick:(id)sender {
vc1 *vc1Object = [[vc1 alloc] init]; //create object
[email protected]"New Text"; //set new text
}
我上面的代碼不工作,因爲它是C ^重新創建一個新實例,而不是訪問原始屬性。但是,我不知道如何爲IBOutlets創建getter/setter,或者如果有其他方法。如何從第二個視圖控制器更改UIlabel上的文本?
任何幫助,將不勝感激。先謝謝你。
你可以使用這個: - 1'NSNotificationCenter' 2.'KVO' 3.'Delegates' 4。 'Block' – pkc456
你如何管理v1和v2?你是否從v2轉到v1?或者這個v1和v2是uiviewcontainers?或者您從v2發回v1 - 方法將會不同 – Nazir
@ pkc456我最終使用NSNotificationCenter,感謝您的幫助! – hmzfier