当 UIView 的子视图不起作用时,在 UIButton 上点击方法
Tap method on UIButton when its a sub view of UIView not working
我在点击 UIButton 时遇到 EXC_BAD_ACCESS 错误。如果我将逻辑移至 RootViewController.m
它会起作用,但在我的 PanelViewController.m
中实现时则不会。我不明白为什么这行不通,我错过了什么,我怎样才能让它发挥作用?
RootViewController.m
#import "RootViewController.h"
#import "PanelViewController.h"
@interface ListViewRootController ()
@end
@implementation ListViewRootController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
[self.view setBackgroundColor:[UIColor blueColor]];
PanelViewController *panelView = [[PanelViewController alloc] init];
[self.view addSubview:panelView.view];
}
@end
PanelViewController.m
#import "PanelViewController.h"
@interface PanelViewController ()
@end
@implementation PanelViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *topBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
topBar.backgroundColor = [UIColor whiteColor];
UIButton *mapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, topBar.frame.size.height)];
[mapButton setTitle:@"Map" forState:UIControlStateNormal];
[mapButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[mapButton addTarget:self action:@selector(mapViewToggle:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:topBar];
[topBar addSubview:mapButton];
}
- (void)mapViewToggle:(UIButton *)sender{
NSLog(@"hello, is it me youre looking for");
}
@end
是的,不行,panelView controller 在此之后发布
PanelViewController *panelView = [[PanelViewController alloc] init];
你应该在 RootViewController 的 .h 中有一个 var,类似于
@interface RootViewController : UIViewController {
PanelViewController *panelView;
}
然后:
panelView = [[PanelViewController alloc] init];
[self.view addSubview:panelView.view];
我在点击 UIButton 时遇到 EXC_BAD_ACCESS 错误。如果我将逻辑移至 RootViewController.m
它会起作用,但在我的 PanelViewController.m
中实现时则不会。我不明白为什么这行不通,我错过了什么,我怎样才能让它发挥作用?
RootViewController.m
#import "RootViewController.h"
#import "PanelViewController.h"
@interface ListViewRootController ()
@end
@implementation ListViewRootController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
[self.view setBackgroundColor:[UIColor blueColor]];
PanelViewController *panelView = [[PanelViewController alloc] init];
[self.view addSubview:panelView.view];
}
@end
PanelViewController.m
#import "PanelViewController.h"
@interface PanelViewController ()
@end
@implementation PanelViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *topBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
topBar.backgroundColor = [UIColor whiteColor];
UIButton *mapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, topBar.frame.size.height)];
[mapButton setTitle:@"Map" forState:UIControlStateNormal];
[mapButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[mapButton addTarget:self action:@selector(mapViewToggle:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:topBar];
[topBar addSubview:mapButton];
}
- (void)mapViewToggle:(UIButton *)sender{
NSLog(@"hello, is it me youre looking for");
}
@end
是的,不行,panelView controller 在此之后发布
PanelViewController *panelView = [[PanelViewController alloc] init];
你应该在 RootViewController 的 .h 中有一个 var,类似于
@interface RootViewController : UIViewController {
PanelViewController *panelView;
}
然后:
panelView = [[PanelViewController alloc] init];
[self.view addSubview:panelView.view];