0

当我尝试从子视图访问主视图控制器中的方法时,我的应用程序崩溃了。

请帮忙。

我的主 ViewController 中有一个 UIButton,它可以更改 UILabel 中的文本,它也在我的主视图中。我在子视图中有一个 UIButton,它应该通过从主 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

视图控制器.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;
}

子视图控制器

子视图控制器.h

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

@interface SubViewController : UIViewController

@property (nonatomic, retain) UIButton *subClassButton;

@end

子视图控制器.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 还很陌生,如果你能提供任何帮助,我将不胜感激。

谢谢

4

3 回答 3

1

您需要从内部调用-setLabelTo您已经存在的实例——目前,您正在创建一个新实例并调用该新实例。ViewController-[SubViewController touchAction:]ViewController-setLabelTo:

为了调用-setLabelToViewController我们需要引用那个ViewController

UIViewController提供了一个属性parentViewController,在理想情况下,它会为您SubViewControllerViewController. 但是,在这种情况下,它没有提供这样的参考。您将您SubViewController的视图添加为您的子视图ViewController,但您没有将您的视图添加SubViewController为您的子视图控制器ViewController。(另请参阅此问题接受的答案。)要解决此问题,请在-[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];

此时,您SubViewController是的子视图控制器,ViewControllerSubViewController的视图是您的视图的子ViewController视图。

此时,您可以将实现更改-[SubViewController touchAction:]

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

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

这将调用-setLabelTo:您的原始ViewController实例,而不是创建一个新实例并导致崩溃。

于 2013-01-07T23:09:39.153 回答
0

您必须为 subviewcontroller 获取父视图控制器:self.parentViewController 您在 subViewController 中的 touchAction 中所做的是创建一个新的 View Controller。

于 2013-01-07T23:08:24.430 回答
0

看看这些行:

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

您正在实例化 ViewController,但未viewDidLoad调用。在那里你实例化你想在 usingsetLabelTo方法上设置标题的 UILabel 。

viewDidXXXX 和 viewWillXXXX Storyboard在一些 UINavigationController 中使用时会自动调用。

于 2013-01-07T23:11:18.107 回答