views:

151

answers:

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
}  

Getting this error:

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). 
A: 

Is buttons an array that you initialized (I'm assuming since you are using addObject which is only for arrays)? You say that it's supposed to add it to a scroll list (i'm guessing you meant a scrollview), but the error you are giving makes it seem like you are passing it into a string object.

To add to a scrollview, you would do:

[someScrollview addSubview:btn];

Also, I don't see anywhere that you set the frame of the object for when it gets added to the scrollview. That's something you would need to do before adding it as a subview.

Works just fine for me (note, you should be initializing and releasing the mutable array somewhere, I just did this quickly):

Console output = 2010-07-29 15:54:54.563 Torch[52222:207] Yep Hi and 0

Code that I used:

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);

}
iWasRobbed
My bad. Im newbie. Yes buttons array initlized during init. Yes [scrollview addSubview:btn]. The problem is when i press the dynamically added button.
Lorem Ipsum
No prob, just trying to get as much info as possible :) See my updated post, this works just fine for me.. Instead of adding it to a scrollview I just added it to a view real quick, but there is no difference in the two. I made my button title "Hi"
iWasRobbed
Didnt work for some reason. Thanks though. See answer above.
Lorem Ipsum
A: 

I did add [self retain]; before adding the action to the button and now it works!

-(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

Any reason why?
self is the UIViewController that handles everything (at least I thought)

Lorem Ipsum
Calling `[self retain];` opens the door for a big memory leak since you are bumping your view controller's `retainCount` up by 1 and never releasing the object again. That has nothing to do with creating a button dynamically.
iWasRobbed
Recommendations then?
Lorem Ipsum