2013-01-07 105 views
0

當我嘗試從子視圖訪問我的主ViewController中的方法時,我的應用程序崩潰。Xcode:如何從子類訪問方法?

請幫忙。

我在我的主ViewController中有一個UIButton,它改變了UILabel中的文本,這也在我的主視圖中。在SubView中有一個UIButton,它應該通過訪問主ViewController中的相同方法來再次更改字符串。但該應用程序崩潰。

這裏是我的代碼:

主視圖控制器

ViewController.h

#import <UIKit/UIKit.h> 
#import "SubViewController.h" 

@interface ViewController : UIViewController 

@property (nonatomic, strong) UILabel *label; 
@property (nonatomic, retain) UIButton *mainClassButton; 

- (void) setLabelTo:(NSString *)copy; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize label; 
@synthesize mainClassButton; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 30.0, 200.0, 30.0)]; 
    self.label.text = @"Let's Go Mets!"; 
    [self.view addSubview:label]; 

    mainClassButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    mainClassButton.frame = CGRectMake(30.0, 70.0, 200.0, 30.0); 
    [mainClassButton setTitle:@"Main Class Button" forState:UIControlStateNormal]; 
    [mainClassButton addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:mainClassButton]; 

    SubViewController *subViewController = [[SubViewController alloc] init]; 
    subViewController.view.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:subViewController.view]; 

    [self.view bringSubviewToFront:self.mainClassButton]; 
} 

- (IBAction)touchAction:(UIButton *) sender 
{ 
    [self setLabelTo:@"Here we go Yankess!"]; 
} 

- (void) setLabelTo:(NSString *)copy 
{ 
    self.label.text = copy; 
} 

子視圖控制器

個SubViewController.h

#import <UIKit/UIKit.h> 
#import "ViewController.h" 

@interface SubViewController : UIViewController 

@property (nonatomic, retain) UIButton *subClassButton; 

@end 

SubViewController.m

@implementation SubViewController 

@synthesize subClassButton; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    subClassButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    subClassButton.frame = CGRectMake(30.0, 115.0, 200.0, 30.0); 
    [subClassButton setTitle:@"Sub Class Button" forState:UIControlStateNormal]; 
    [subClassButton addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:subClassButton]; 

} 

- (IBAction)touchAction:(UIButton *) sender 
{ 
    ViewController *vc = [[ViewController alloc] init]; 

    [vc setLabelTo:@"If you ask me, I prefer the White Sox."]; 
} 

我仍然是一種新的Xcode和Objective-C,我會感激你可以提供任何幫助。

感謝

+0

您可以包括控制檯輸出,所以我們可以看到你的錯誤? –

回答

1

你需要從-[SubViewController touchAction:]中調用-setLabelTo您現有的ViewController實例--at此刻,你正在創建的ViewController一個新實例,並在該新實例調用-setLabelTo:

爲了在ViewController上致電-setLabelTo,我們需要參考ViewController

UIViewController提供財產parentViewController其中,在理想情況下,將提供您的SubViewControllerViewController參考。但是,在這種情況下,它不提供這樣的參考。您將SubViewController的視圖添加爲您的ViewController的子視圖,但您的確要而不是將您的SubViewController添加爲您的ViewController的子視圖控制器。 (另見this questionthe accepted answer。)要解決此問題,在-[ViewController viewDidLoad],添加行

[self addChildViewController:subViewController]; 

SubViewController *subViewController = [[SubViewController alloc] init]; 

給你

SubViewController *subViewController = [[SubViewController alloc] init]; 
[self addChildViewController:subViewController]; 
subViewController.view.backgroundColor = [UIColor clearColor]; 
[self.view addSubview:subViewController.view]; 

在這一點上,你SubViewControllerViewController子視圖控制器和您的SubViewController的觀點是您的ViewController的觀點的子視圖。

在這一點上,你可以改變你的-[SubViewController touchAction:]實施

- (IBAction)touchAction:(UIButton *) sender 
{ 
    ViewController *vc = self.parentViewController; 

    [vc setLabelTo:@"If you ask me, I prefer the White Sox."]; 
} 

它將調用-setLabelTo:你原來的ViewController而不是創建一個新的實例,並導致飛機墜毀。

0

你必須得到該subviewcontroller父視圖控制器:self.parentViewController你在touchAction在subViewController做的是創建一個新的視圖控制器。

0

看看這些行:

ViewController *vc = [[ViewController alloc] init]; 
[vc setLabelTo:@"If you ask me, I prefer the White Sox."]; 

你實例化視圖控制器,但viewDidLoad不叫。在那裏你實例化你想使用setLabelTo方法設置標題的UILabel。當他們在Storyboard或在某些UINavigationController的使用

的viewDidXXXX和viewWillXXXX被自動調用。