【问题标题】:Xcode: How do I access methods from Sub Classes?Xcode:如何从子类访问方法?
【发布时间】:2012-12-21 18:07:44
【问题描述】:

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

请帮忙。

我的主 ViewController 中有一个 UIButton,它可以更改 UILabel 中的文本,它也在我的主视图中。我在子视图中有一个 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 的新手,如果你能提供任何帮助,我将不胜感激。

谢谢

【问题讨论】:

  • 能否包含控制台输出,以便我们查看您得到的错误?

标签: objective-c ios xcode oop


【解决方案1】:

您需要在-[SubViewController touchAction:] 中对现有的ViewController 实例调用-setLabelTo——目前,您正在创建ViewController 的新实例并在该新实例上调用-setLabelTo:

为了在ViewController 上调用-setLabelTo,我们需要对ViewController 的引用。

UIViewController 提供了一个属性parentViewController,在理想情况下,它会为您的SubViewController 提供对您的ViewController 的引用。但是,在这种情况下,它没有提供这样的参考。您将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."];
}

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      看看这些行:

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

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

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

      【讨论】:

        猜你喜欢
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-14
        • 1970-01-01
        相关资源
        最近更新 更多