2014-01-21 62 views
0

不工作(EXC_BAD_ACCESS)我做了我自己的類UIView的,包括的UIButton和它的方法:的UIButton中的ViewController

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]; 
} 

在視圖控制器我做了MyClass的實例,並調用的方法顯示視圖

MyClass* mySort = [[myClass alloc] initFor: rooms]; 
[mySort ShowBarOnView:self AtPoint:CGPointMake(0, 50)]; 

所以,我使用按鈕視圖,但是當被按下時,該應用程序崩潰和節目: EXC_BAD_ACCESS(碼= 1,地址=爲0x0)

有什麼不對?

謝謝。

+0

調試器可以告訴你這一點。順便說一句,如果你不使用ARC,那麼'_sortButton = [UIButton buttonWithType:UIButtonTypeSystem]'行缺少'retain'。 – 2014-01-21 23:37:00

+1

請更改您的班級名稱以獲得更好的做法,班級名稱應爲大寫字母'MyClass.m'而不是'myClass。m' – Malloc

+0

可能是不相關的,但是你應該使用'self.'來訪問setter,getter,init和alloc方法以外的任何地方的實例變量。 – nhgrif

回答

0

我解決了這個問題,通過在ViewController.h

@property (strong, nonatomic) MyClass* mySort; 

將在 和改變我在ViewController.m代碼

_mySort = [[SortAndSeach alloc] initFor:for_what]; 
[_mySort ShowBarOnView:self AtPoint:CGPointMake(0, 50)]; 

感謝所有!

+0

如果您的問題已得到解答,即使是您自己,您也應該將其標記爲「接受此[您的]答案」。這樣它應該可以幫助其他可能有這個問題的人!乾杯 –

0

如果發佈的代碼片段與寫入完全一樣,問題是以下幾行從不執行。 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內存地址的東西,你都試圖在沒有內存地址的對象上調用一個方法......它從來沒有被分配過。

+0

對不起,我誤導了你。這些線是我最初的方法。我更正了我的問題中的代碼 – user2976219

1

是的,我有同樣的問題!

我有我的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

因此該解決方案在這裏我們的問題是:

#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]; 
} 
相關問題