UIButton Event in iphone - iphone

I am new to iphone.I created a UIButton programmatically,which is used to exit from subView to PreviousView.
For this, I written code as follows:
-(IBAction)button99:(id)sender
{
[myview1 removeFromSuperview];
}
But the above event is not fired.What can i do for this?
Can any one suggest me proper code.
Thanks in Advance.

Add this to your code,
[yourButton addTarget:self action:#selector(button99:) forControlEvents:UIControlEventTouchUpInside];

To use the button with (IBAction) you should use Interface Builder to link that method to the actual button. If you link that to the button the method will be fired.
Be sure to declare the IBAction in the .h file also..
And indeed Simon is correct when you wish to do it programmatically..!

Related

set UIButton's image in interface builder for differen state

I am facing very strange issue.
In interface builder I am trying to set images for "normal" state config and for "selected" state config. but by clicking the button my image is not getting changed.
even I write the code in viewDidLoad method.
[btnCheckBoxMale setImage:[UIImage imageNamed:#"blankcheckbox.png"] forState:UIControlStateNormal];
[btnCheckBoxMale setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateSelected];
but my images are not getting changed on cliking on it
can anyboday say what is the problem?
Set the image to different states in interface builder instead of setting it in code. Please see the screenshot for how to set it. Try this & if you are facing any problem then ask me.
It will work if you set manually in button action:
btnCheckBoxMale.selected = YES;
Because by default UIButton has only two states - Normal and Highlighted. And other states like selected and disabled you have to control manually.
just set selected when you call the method on button click event.. for example..
-(IBAction) btnCheckBoxMale_Clicked:(id)sender{
[btnCheckBoxMale setSelected:YES];
}
-(IBAction) btnCheckBoxMale_Clicked:(id)sender{
UIButton * btnCheckBoxMale = (UIButton *)sender;
[btnErase setSelected:! btnCheckBoxMale];
}

Issues in setting UIButton title in its click event

I'm trying to change the title of a button when the user press on it with the following code:
- (IBAction) hideKB: (UIButton *) sender {
sender.titleLabel.text = #"↓";
}
But when I click the app crashes and I can't understand why.
Removing the sender stuff, the button works with no problems.
The correct method signature for an action is
- (IBAction)action:(id)sender
Your app is crashing because your object is being sent a message it doesn't understand.
Try replacing your code with something along the lines of:
- (IBAction)hideKB:(id)sender {
UIButton *button = (UIButton *)sender;
[button setTitle:#"↓" forState:UIControlStateNormal];
}
As you may notice I've also changed the line that sets the button title. This is because you should never manipulate a UIButton's titleLabel property directly, but rather you should be using the appropriate setter method as shown above.
Edit: To clarify, most controls will allow you to use dot notation to edit the text property of their titleLabel. However, UIButton instances support different titles (as well as images and background images) depending on the state they're in.
If you're wondering why a UIButton could be in one of several different states, a good example you often see is buttons that are "greyed out". What this means is that those buttons are in the UIControlStateDisabled state. You can find a list of all the possible states for a control in the documentation.
Try using
- (void)setTitle:(NSString *)title forState:(UIControlState)state
e.g.
[sender setTitle:"↓" forState:UIControlStateNormal];
you can try with:
[(UIButton *)sender setTitle: #"↓"" forState: UIControlStateNormal];
For more information, you can refer to setTitle:forState: in UIButton Class Reference
Hope this helps.

Setting up layout/events on iPhone

I am using Open Source toolchain to compile my iPhone apps. So I have no Interface Builder or XCode. How would I setup the layout of widgets like UIButton, UITextView, etc. Also, how would I add an event handler to those UI widgets? Please remember that I don't have Interface Builder or XCode.
I suspect you're going to run into trouble eventually if you're not using Xcode. However, it is definitely possible to do everything iPhone without using Interface Builder. You just end up defining a lot of rectangles and pixel constants in your code, and calling (e.g.) addTarget to hook actions up to methods.
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(MYBUTTON_X, MYBUTTON_Y, MYBUTTON_WIDTH, MYBUTTON_HEIGHT)];
[myButton addTarget:self action:#selector(foo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
[myButton release];
That creates a button and has it call your "foo" method when pushed. Anything that can be done in IB can be done in plain code (though it's often more tedious to do it that way.)

Two Problems I'm having with UIButton and UIView

I haven't been programming on the iPhone for very long, but I'm picking it up slowly by googling problems I get. Unfortunately I haven't been able to find an answer for these.
I have started a new View-based application in Xcode 3.2.2 and immediately added the following files: myUIView.m and myUIView.h, which are subclasses of UIView. In Interface Builder, I set the subclass of the default UIView to be myUIView.
I made a button in the drawRect method.
Problem one: The title of the button only appears AFTER I click the screen, why?
Problem two: I want the button to produce the modalview - is this possible?
The code is as follow:
#import "myUIView.h"
#implementation myUIView
- (void)drawRect:(CGRect)rect {
// Drawing code
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0,0,100,100);
[button setTitle:#"butty" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
-(void)buttonPressed:(id)sender{
NSLog(#"Button pressed");
//present modal view somehow..?
}
I can't see how to post attachments, but if anyone thinks it will help I can upload the source.
Many thanks,
Andy
drawRect: is for custom drawing. You won't often need to do that. Your button won't be drawn until it gets sent drawRect:, probably in the next iteration of the run loop (ie, when you click on the view).
If you want a button on your view, either drag it there using IB, or move the code from drawRect: to viewDidLoad.
You present a modal view from a view controller by:
[self presentModalViewController:viewController animated:YES];
Do this in the action method your button sends to your view controller. This should indicate to you that you need to take a look at your application design.

how can i fire button action event without any user action iphone xcode?

how can i fire button action event without any user action?
pls give me any sample..
UIButtons are subclass of UIControl. They use the sendActionsForControlEvents: method to send out events. You can call that method on the button (or control) to have the actions sent.
[theButton sendActionsForControlEvents: UIControlEventTouchUpInside];
If user dont want to fire action on button,why do you show button,without showing button,u can do action.I think u need dynamic code (without interface builder),the code is
[button1 addTarget:self action:#selector(action1:) forControlEvents:UIControlEventTouchUpInside];
-(void) action1:(id)sender;
{
}
hope this will help....