2010-07-29 58 views
2
//adds a button to scroll list 
-(void) addNode:(NSString *) atitle{ 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btn setTitle:atitle forState:UIControlStateNormal]; 
    [btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; // handle touch 
    [buttons addObject:btn]; 
.... 
} 

//a data button was pressed 
-(void) buttonTouched:(id)sender{ 
    int index = [buttons indexOfObject:sender]; 
    NSString *key = [sender titleLabel].text; 
} 

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> 

-(id) init{ 
    self = [super init]; 
    ... 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
[super viewDidLoad]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"ViewDidLoad" object:nil]; 
} 

-(void) awakeFromNib{ 
[self loadData]; // loads some data 
    //calls addNode for a bunch of dynamic data 
} 

收到此錯誤:EXC_BAD_ACCESS無法識別的選擇發送到實例

2010-07-29 10:19:45.428 DataManager[2945:207] -[NSCFString buttonTouched:]: unrecognized selector sent to instance 0x593abb0 
2010-07-29 10:19:45.429 DataManager[2945:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString buttonTouched:]: unrecognized selector sent to instance 0x593abb0' 
*** Call stack at first throw: 
( 
0 CoreFoundation      0x02399919 __exceptionPreprocess + 185 
1 libobjc.A.dylib      0x024e75de objc_exception_throw + 47 
2 CoreFoundation      0x0239b42b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
3 CoreFoundation      0x0230b116 ___forwarding___ + 966 
4 CoreFoundation      0x0230acd2 _CF_forwarding_prep_0 + 50 
5 UIKit        0x002bde14 -[UIApplication sendAction:to:from:forEvent:] + 119 
6 UIKit        0x003476c8 -[UIControl sendAction:to:forEvent:] + 67 
7 UIKit        0x00349b4a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 
8 UIKit        0x003486f7 -[UIControl touchesEnded:withEvent:] + 458 
9 UIKit        0x00535070 _UIGestureRecognizerUpdateObserver + 3687 
10 CoreFoundation      0x0237ad1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27 
11 CoreFoundation      0x0230f987 __CFRunLoopDoObservers + 295 
12 CoreFoundation      0x022d8c17 __CFRunLoopRun + 1575 
13 CoreFoundation      0x022d8280 CFRunLoopRunSpecific + 208 
14 CoreFoundation      0x022d81a1 CFRunLoopRunInMode + 97 
15 GraphicsServices     0x02bfe2c8 GSEventRunModal + 217 
16 GraphicsServices     0x02bfe38d GSEventRun + 115 
17 UIKit        0x002cbb58 UIApplicationMain + 1160 
18 DataManager       0x00002940 main + 102 
19 DataManager       0x000028d1 start + 53 
) 
terminate called after throwing an instance of 'NSException' 
Program received signal: 「SIGABRT」. 
kill 
kill 

The Debugger has exited due to signal 15 (SIGTERM).The Debugger has exited due to signal 15 (SIGTERM). 

回答

0

buttons你初始化(我假設,因爲你正在使用addObject這是唯一的數組)的數組?你說它應該把它添加到一個滾動列表中(我猜你的意思是一個滾動視圖),但是你給的錯誤使得它看起來像你將它傳遞給一個字符串對象。

要添加到滾動視圖,你會怎麼做:

[someScrollview addSubview:btn]; 

而且,我沒有看到任何地方,你設置的對象,當它被添加到滾動視圖的框架。這是你在將它作爲子視圖添加之前需要做的事情。

工作得很好,對我來說(注意,你應該初始化和地方釋放可變數組,我只是做這個快):

控制檯輸出= 2010-07-29 15:54:54.563火炬[52222 :207]是的Hi和我用0

代碼:

NSMutableArray *buttons; // in your .h interface 

@property (nonatomic, retain) NSMutableArray *buttons; // in your .h 

@synthesize buttons; // in your .m 

(...)

-(IBAction) pushMyButtons { 
buttons = [[NSMutableArray alloc] init]; 

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[btn setTitle:@"Hi" forState:UIControlStateNormal]; 
[btn setFrame:CGRectMake(65, 300, 200, 23)]; 
[btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; // handle touch 
[buttons addObject:btn]; 
[self.view addSubview:btn]; 
} 

//a data button was pressed 
-(void) buttonTouched:(id)sender{ 

int index = [buttons indexOfObject:sender]; 
NSString *key = [sender titleLabel].text; 
NSLog(@"Yep %@ and %d", key, index); 

} 
+0

我的不好。我是新手。初始化期間是按鈕陣列啓動。是[scrollview addSubview:btn]。問題是當我按動態添加按鈕。 – 2010-07-29 19:36:01

+0

沒有概率,只是試圖獲得儘可能多的信息:)看到我更新後的帖子,這對我來說很好。不是將它添加到scrollview中,而是將它添加到視圖中,但實際上沒有區別在兩個。我製作了我的按鈕標題「Hi」 – iwasrobbed 2010-07-29 19:59:56

+0

由於某種原因沒有工作。不過謝謝。見上面的答案。 – 2010-07-30 14:37:19

1

我在添加動作到按鈕之前添加了[self retain];,現在它可以工作!

-(void) addNode:(NSString *) atitle{ 
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btn setTitle:atitle forState:UIControlStateNormal]; 
    [self retain]; 
    [btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; // handle touch 

任何原因爲什麼?
自我是如果你正在使用ARC編譯並通過編程設置選擇,處理一切(至少我認爲)

+0

調用'[self retain];'打開大內存泄漏的大門,因爲您將視圖控制器的'retainCount'加1,並且永遠不會再次釋放該對象。這與動態創建按鈕無關。 – iwasrobbed 2010-07-30 14:59:30

+0

建議呢? – 2010-07-30 16:14:28

1

UIViewController

[_backButton addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside]; 

的EXC_BAD_ACCESS因爲我創造了一個父時,我然後將子視圖添加到另一個視圖。子視圖(使用按鈕)有其父級版本。一切都很好,直到按鈕被選中,然後崩潰。

選擇器的任何異常都可能是類發佈問題。不應將保留用作工作(當然,在ARC編譯中,您不能使用保留)。代碼應該在可能釋放對象的父對象的函數中檢查可能的寡居指針。

希望有所幫助。

相關問題