当我尝试从 subview 访问主视图 Controller 中的方法时,我的应用程序崩溃了。
请帮忙。
我的主 ViewController 中有一个 UIButton,它可以更改 UILabel 中的文本,该 UILabel 也在我的主视图中。我在 subview 中有一个 UIButton,应该通过从主 ViewController 访问相同的方法来再次更改字符串。但应用程序崩溃了。
这是我的代码:
主视图 Controller
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;
}
subview Controller
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 还比较陌生,如果您能提供任何帮助,我将不胜感激。
谢谢
最佳答案
您需要在 -[SubViewController touchAction:]
中对现有的 ViewController
实例调用 -setLabelTo
- 此时,您将创建 ViewController
的新实例,并在该新实例上调用 -setLabelTo:
。
为了在 ViewController
上调用 -setLabelTo
,我们需要对该 ViewController
的引用。
UIViewController
提供了一个属性 parentViewController
,在理想情况下,该属性将为您的 SubViewController
提供对 ViewController
的引用>。然而,在这种情况下,它不提供这样的引用。您将 SubViewController
的 View 添加为 ViewController
的 subview ,但您不将 SubViewController
添加为ViewController
的 subview Controller 。 (另请参阅 this question 和 the 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];
此时,您的 SubViewController
是 ViewController
的 subview Controller ,而 SubViewController
的 View 是 ViewController 的 subview >ViewController
的 View 。
此时,您可以将 -[SubViewController touchAction:]
的实现更改为
- (IBAction)touchAction:(UIButton *) sender
{
ViewController *vc = self.parentViewController;
[vc setLabelTo:@"If you ask me, I prefer the White Sox."];
}
它将在原始 ViewController
上调用 -setLabelTo:
,而不是创建新实例并导致崩溃。
关于objective-c - Xcode:如何访问子类中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14205658/