I found some answers to my question but it still does not work. I have setup a button in IB and I want to setup the action and target by code(just for learning). I have a function in my MainViewController:
- (IBAction) onSpecialCase:(id)sender {
NSLog(#"It's working");
}
In the viewDidLoad function I do this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[myButton setTarget:self];
[myButton setAction:#selector(onSpecialCase:)];
}
Xcode already marks these lines telling me: "UIButton may not respond to 'setTarget'". When running the app anyways it terminates saying:
-[UIRoundedRectButton setTarget:]: unrecognized selector sent to instance 0x4b47e30
2011-06-29 08:12:22.465 FirstTry[3765:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIRoundedRectButton setTarget:]: unrecognized selector sent to instance 0x4b47e30'
That's what I could expect from the XCode warning, but why is that? Every example I find on the web is exactly like mine. Adding the actionMethode to the button via IB works like expected. What am I doing wrong?
first check if you have declared the button as an outlet, second you should check if you connect the outlet to the button or use what I usually do is to add new button in code
UIButton *Button=[UIButton buttonWithType:UIButtonTypeCustom] ;
Button.frame=CGRectMake(0,0,30,30); // place your button
[Button addTarget:self action:#selector(processButtonCLick) forControlEvents:UIControlEventTouchUpInside];
[Button setImage:[UIImage imageNamed:#"imageoricon.png"] forState:UIControlStateNormal];
[self.view addSubview:Button];
It may help you.
UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[sendButton setTitle:#"Send" forState:UIControlStateNormal];
[sendButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
sendButton.frame = CGRectMake(0, 3, 67, 37);
[sendButton addTarget:self action:#selector(sendButtonClicked) forControlEvents:UIControlEventTouchUpInside];
Related
Thanks for reading; this is my problem, I'm trying to hook up code I have written to buttons which already exist in the storyboard but I can't find a reference or identifier for the button(s).
I want to be able to modify buttons which have been created on the storyboard and style/give actions in my code.
So I would like something I can reference like so:
UIButton *subBtn;
where subBtn is the button's reference/identifier.
tldr; where do you find the reference for buttons in xcode (v6+) storyboard?
For those who come after me:
UIButton *subBtn = (UIButton *) [self.view viewWithTag:9];
The Line above allows you to reference a button with an "tag" which you can find in the attributes tab in the storyboard.
By using this it allows you to change attributes like below, to format your buttons:
[subBtn addTarget:self action:#selector(submitHit) forControlEvents:UIControlEventTouchUpInside];
UIImage *UISubImg = [UIImage imageNamed: #"submit_button.png"];
UIImage *UISubImg2 = [UIImage imageNamed: #"submit-play-icon.png"];
[subBtn setTitle:#"SUBMIT" forState:UIControlStateNormal];
subBtn.frame = CGRectMake(20, 400, 280, 65);
[self.view addSubview:subBtn];
[subBtn setBackgroundImage:UISubImg forState:UIControlStateNormal];
[subBtn setImage:UISubImg2 forState:UIControlStateNormal];
SubmitHit is a method.
I am creating a calculator game, in which I have to popup a UIAlertView, I customized the UIAlertView and put a UITableView programatically into it, When the user tap any of its cell I show a UIView on the top of UITableView, this view has a button, when I clicked that button the event does not fires, I have also installed it manually, but it doesn't work, previously the attachment of event with button is generating an error, for that I uninstalled the event in dealloc, and fix the problem, but after it still the event is not firing.
i was have case such your and i have to add it pragmatically
in .h declare
UIButton *btn;
in .m add it to the UIView
UIView *TempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
if (btn) {
[btn removeFromSuperview];
// [btn release]; // for not using arc
}
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10, 10, 25, 25)];
[btn addTarget:self action:#selector(ClickMe:) forControlEvents:UIControlEventTouchUpInside];
[TempView addSubview:btn];
[self.view addSubview:TempView];
and the action is
-(void)ClickMe:(id)sender{
//here is an action
}
it's working for me
I know how to do this in iOS but cannot figure it how to it in Cocoa.
I want to capture keyboard events and I think I need to override the acceptsFirstResponder method to accomplish that (keyDown method being triggered). So I created a class extending NSCustomView and tried to add it in the main Window but I just cannot understand how to do it. So far I added a Custom View to the main View then tried to add it programmatically like:
TestView *view = [[TestView alloc] init];
[[_window contentView] addSubview:view];
but this is not working. So how can I do this?
To see if the view has been added to a window, you can override the view's viewDidMoveToWindow method and log the value of [self window] to check (if it's nil then the view has been removed from a window):
- (void)viewDidMoveToWindow
{
NSLog(#"window=%p", [self window]);
[super viewDidMoveToWindow];
}
You should be subclassing NSView, not NSCustomView, and initWithFrame is the designated initializer for NSView, not init.
Try:
TestView *view = [[TestView alloc] initWithFrame:NSMakeRect(0, 0, 100, 200)];
[[_window contentView] addSubview:view];
I'm getting my feet wet with objective-C. And thought I'd play with iphone development.
I have a Page Controller.h and .m
Inside my implementation file I've thrown a UIButton inside the viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
// content that loads in view
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(10, 160, 300, 44);
[myButton setTitle:#"Press Me!" forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont systemFontOfSize:14];
// add to a view
[self.view addSubview:myButton];
}
So far it's showing fine. This makes me ask about the difference between loadView vs viewDidLoad.
Also, I want to run my own function when the button is pressed, but I constantly get an executable error from xcode.
Here's what I've interjected within the code above:
- (void)viewDidLoad
{
[super viewDidLoad];
// content that loads in view
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(10, 160, 300, 44);
[myButton setTitle:#"Press Me!" forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont systemFontOfSize:14];
//added this
[myButton addTarget:self action:#selector(runClick) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[self.view addSubview:myButton];
}
-(void)runClick{
NSLog(#"does this work?");
}
Does anything seem out of place to anyone?
Also, In the PageController.h I added: -(void)runClick;
Here's the Error I catch:
2012-03-17 16:04:30.077 Blah[510:207] -[__NSCFString runClick]: unrecognized selector sent to instance 0x6a0b4f0
About the difference between loadView and viewDidLoad:
According to Apple, the controller (not you!) calls this method when it's view is requested and it's nil. So, it will create and load the view.
You can override loadView to create your own view (if you don't have a nib for that controller)
If you want to add something to the view created by a view controller with a nib file do it on viewDidLoad, and remove it on viewDidUnload.
About the error, can you provide more information? Are you debugging? What's the line it crashes?
Did you declare -(void)runClick in your .h file? (obvious question).
Also, if you want to get access to the control's properties (say if it were a slider, etc.), you need to add the sender bit:
-(void)runClick (id)sender
I believe you need to add a colon after runClick as in:
action:#selector(runClick:)
I have a popover view which is called with:
Info *infoview = [[Info alloc]init];
pop = [[UIPopoverController alloc]initWithContentViewController:infoview];
[pop setDelegate:self];
[pop presentPopoverFromRect:CGRectMake(-60, 30, 400, 400) inView:bigMenuView permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[infoview release];
In this popoverview is (see in the code) an info.xib which has some UIBUttons.
Now I want to change something. in my "mainview" after I click on the Button.
Note: I can access my IBAction Method in the "First Responder" but the is no action.
In order to do what you want, declare a property on the button in Info, something like :
#property(retain) IBOutlet UIButton *myButton;
Now synthesize it, in implementation part. In order for the button to send the correct action to the correct target, you should do like :
Info *infoview = [[Info alloc]init];
// set the action/target here
[infoView.myButton addTarget:bigMenuView
action:#selector(theSELYouWant)
forControlEvents:UIControlEventTouchUpInside];
pop = [[UIPopoverController alloc] initWithContentViewController:infoview];
[pop setDelegate:self];
[pop presentPopoverFromRect:CGRectMake(-60, 30, 400, 400)
inView:bigMenuView
permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[infoview release];
I supposed that the main view is bigMenuView.
This will make the button sending a theSELYouWant message to bigMenuView each time it is touched.