[英]UIButton from my class not working (EXC_BAD_ACCESS) in ViewController
我使用UIView制作了自己的类,其中包括UIButton及其方法:
MyClass.h
@property (strong, nonatomic) UIView* mainBack;
@property (strong, nonatomic) UIButton* sortButton;
MyClass.m
- (id) initFor: (int) object {
if (self = [super init]) {
_mainBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
_sortButton = [UIButton buttonWithType:UIButtonTypeSystem];
_sortButton.frame = CGRectMake(10.0, 5.0, 60.0, 40.0);
[_sortButton addTarget:self action:@selector(ChangeSort) forControlEvents:UIControlEventTouchUpInside];
[_mainBack addSubview:_sortButton];
}
return self;
}
- (void) ChangeSort {
NSLog(@"change");
}
- (void) ShowBarOnView: (UIViewController*) view AtPoint: (CGPoint) point{
[_mainBack setFrame:CGRectMake(point.x, point.y, _mainBack.frame.size.width, _mainBack.frame.size.height)];
[view.view addSubview:_mainBack];
}
在ViewController中,我创建了myClass的实例,并调用了显示视图的方法
MyClass* mySort = [[myClass alloc] initFor: rooms];
[mySort ShowBarOnView:self AtPoint:CGPointMake(0, 50)];
因此,我获得了带有按钮的视图,但是当按下按钮时,应用程序崩溃并显示:EXC_BAD_ACCESS(代码= 1,地址= 0x0)
怎么了?
谢谢。
是的,我有同样的问题!
我让MainViewController
用一种方法创建了PlayingViewController
,长话短说就是问题所在。
SOFMainVC.h
SOFMainVC.m
#import "SOFPlayingVC.h" ...
- (void)someMethodBeingCalled {
SOFPlayingVC *playingVC = [[SOFPlayingVC alloc] init];
[self.view addSubview:playingVC.view];
}
SOFPlayingVC.m
...
...
- (void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 100, 50)];
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubView:button];
...
...
}
如果我们将重点放在工具中的SOFPlayingVC
,我们将看到控制器一旦实例化就将被垃圾回收。 但是,如果我们只是将其添加到用作@property (nonatomic, strong)
的控制器中,则直到它从SOFMainViewController
出来之前@property (nonatomic, strong)
我们都不会丢失它。
因此,解决此问题的方法是:
#import "SOFPlayingVC.h"
SOFMainVC.h
...
@interface SOFMainVC : UIViewController
@property (nonatomic, strong) SOFPlayingVC *playingVC;
...
@end
SOFMainVC.m
#import "SOFPlayingVC.h" ... ...
- (void)someMethodBeingCalled {
SOFPlayingVC *playingViewController = [[SOFPlayingVC alloc] init];
[self.view addSubview:playingVC.view];
}
如果发布的代码段与编写的代码段完全相同,则问题在于,从不执行以下行。 Xcode应该警告您有关它们的信息:
_mainBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
_sortButton = [UIButton buttonWithType:UIButtonTypeSystem];
_sortButton.frame = CGRectMake(10.0, 5.0, 60.0, 40.0);
[_sortButton addTarget:self action:@selector(ChangeSort)
forControlEvents:UIControlEventTouchUpInside];
[_mainBack addSubview:_sortButton];
如前所述,这些行完全存在于任何方法之外。 最有意义的是创建一个工厂方法:
+ (instancetype) mainViewAtPoint:(CGPoint)point inContext:
(UIViewController*)viewController {
self.mainBack = [[UIView alloc] initWithFrame:CGRectMake(point.x,point.y,
320,50)];
self.sortButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.sortButton.frame = (CGRectMake(10.0, 5.0, 60.0, 40.0);
[self.sortButton addTarget:self action:@selector(ChangeSort)
forControlEvents:UIControlEventTouchUpInside];
[self.mainBack addSubview:self.sortButton];
[viewController.view addSubview: self.mainBack];
}
现在,您要做的所有操作就是分配,初始化,创建并将此视图添加到视图控制器,这很简单:
[myClass mainViewAtPoint:aPoint inContext:aViewController];
EXC_BAD_ACCESS提示了实际上并未创建按钮的明显线索。 它告诉您它正在尝试引用内存位置0x0。 任何时候任何东西试图访问0x0内存地址时,您都试图在没有内存地址的对象上调用方法……它从未被分配过。
我通过在ViewController.h中添加解决了这个问题
@property (strong, nonatomic) MyClass* mySort;
并将我在ViewController.m中的代码更改为
_mySort = [[SortAndSeach alloc] initFor:for_what];
[_mySort ShowBarOnView:self AtPoint:CGPointMake(0, 50)];
谢谢大家!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.