I have the following in my viewdidload:
//Start/Pause Button
UIButton *buttonStart = [UIButton buttonWithType: UIButtonTypeCustom];
buttonStart.frame = CGRectMake(10,100,100,45);
[buttonStart setBackgroundImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[buttonStart addTarget:self action:#selector(pausePlayButtonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: buttonStart];
my selector method:
[self.buttonStart setBackgroundImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
NSLog(#"%#", self.buttonStart);
null is being logged to the console. And needless to say, the image for the button is not being changed.
What is wrong with my thinking?
btw buttonStart is being synthesized and has its own property (retain).
You are using both, an instance variable self.buttonStart accessed via property and a local variable buttonStart. Remove the declaration of that button from your implementation file and change the first line towards this:
self.buttonStart = [UIButton buttonWithType: UIButtonTypeCustom];
No need of property for buttonStart.Add target like this:
[buttonStart addTarget:self action:#selector(pausePlayButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
Now your selected method would be:
-(void)pausePlayButtonTouched:(id)sender
{
UIButton *btnPaused = sender;
NSLog(#"%#", btnPaused);
[btnPaused setBackgroundImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
Are you assigning self.buttonStart anywhere? Did you mean
self.buttonStart = [UIButton buttonWithType: UIButtonTypeCustom];
instead of
UIButton *buttonStart = [UIButton buttonWithType: UIButtonTypeCustom];
You should drop the '.png' tag from your filename, i.e. "pause", not "pause.png".
As far as I can see the assignment of buttonStart is missing.
Did you set self.buttonStart = buttonStart; ?
declaring UIButton *buttonStart = ... will shadow (in your local context) any created member that was created by #property (nonatomic, reatin) UIButton *buttonStart; (if you use LLVM 4.0+ your property declaration will create "UIButton *_buttonStart" as a member)
Related
I want to add a button in a UITableViewCell. This is my code: `
if (indexPath.row==2) {
UIButton *scanQRCodeButton = [[UIButton alloc]init];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:#"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
}`
Now, when I run the app, I see only a blank row ! Any ideas ?
While it's natural to put it in the contentView of the cell, I'm fairly certain that is not the problem (actually, in the past, I've never had subviews displayed correctly in the contentView, so I've always used the cell).
Anyway, the problem involves the first three lines of when you start creating your button. The first two lines are fine, but the code stops working with:
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonWithType: is actually a convenience method to create a button (it's like a compact alloc-init). Therefore, it actually "nullifies" your past two lines (you basically created the button twice). You can only use either init or buttonWithType: for the same button, but not both.
UIButton *scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:#"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
This will work (note that you can use cell.contentView if you wanted). In case you're not using Automatic Reference Counting (ARC), I would like to mention that you don't have to do anything in term of memory management, because buttonWithType: returns an autoreleased button.
UIButton *deletebtn=[[UIButton alloc]init];
deletebtn.frame=CGRectMake(50, 10, 20, 20);
deletebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setImage:[UIImage imageNamed:#"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn addTarget:self action:#selector(DeleteRow:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deletebtn];
or
// Download class and import in your project UIButton+EventBlocks
UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setFrame:CGRectMake(170,5, 25, 25)];
deletebtn.tag=indexPath.row;
[deletebtn setImage:[UIImage imageNamed:#"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn setOnTouchUpInside:^(id sender, UIEvent *event) {
//Your action here
}];
[cell addSubview:deletebtn];
You want to add any custom UI elements to the cell's contentView.
So, instead of [cell addSubview:scanQRCodeButton];
do [cell.contentView addSubview:scanQRCodeButton];
Try adding [cell.contentView addSubview:scanQRCodeButton]; or if you want the button to the left side look at my question at the answer, to move the textLabel to the side. If you want the button to the right then just set it as your accesoryView like this cell.accesoryView = scanQRCodeButton;.
Method setTitle does not work in my code, so I have set by using
[UIButton.titleLabel setText:#""]
instead of using setTitle method.
Please try again with following code:
[scanQRCodeButton.titleLabel setText:#"Hello"];
Then it would work well.
I am trying to create an app for the iPhone and I have a main viewController and a number of other viewControllers. This is because it is a multiview application.
In the ViewController.h and ViewController.m files I have created all my IBActions because they will be shared by all the other views.
Now in firstViewController.m I have created a custom button using the following code:
UIButton *settingsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
settingsButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[settingsButton setTitle:#"Play" forState:UIControlStateNormal];
settingsButton.backgroundColor = [UIColor clearColor];
[settingsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"setButtonNormal.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[settingsButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"setButtonPressed.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[settingsButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[settingsButton addTarget:self action:#selector(loadSettingsView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:settingsButton];
As you can see the action is called "loadSettingsView" which I have correctly declared in ViewController. But it does not work and this is because the above code is in firstViewController.m and NOT in ViewController.m where the IBAction is declared.
Note I have moved the above code in ViewController and the action works, therefore is not a problem of the action. The problem is that I cannot find a way to access the action that has been declared in a different class than the one I am working on.
Can anyone please help me with that?
Thanks a lot
The answer to your question is in the way you declare the target of the action for the button
[settingsButton addTarget:self action:#selector(loadSettingsView) forControlEvents:UIControlEventTouchUpInside];
If this piece of code is located in firstViewController.m and you say that the target is self, it means the compiler must look for the method in the firstViewController class. So to fix this problem you need to point the target of your button action to point to the viewController class. By the way at this point I would like to mention that your naming SUCKS, if you subclass UIViewController don't call it ViewController, make it more meaningful.
The actual fix.
make a global property that will point to ViewController object and call it vc.
[settingsButton addTarget:vc action:#selector(loadSettingsView) forControlEvents:UIControlEventTouchUpInside];
Im new to iphone development.here i added some list of names in pickerview.the action of pickerview was given to button in view. now i want to show.when click the button pickerview list was displayed i selected one name regarding in pickerview that name was displayed at button in view. I dont no how to change the title in button in iphone.
Can any one plz give me information for my problem.
thank you in advance.
You should be able to change the title of a UIButton by calling setTitle. Just remember to set it for all the states of the IUIButton. Here is an example:
[button setTitle:#"Normal Title" forState:UIControlStateNormal];
[button setTitle:#"Highlighted Title" forState:UIControlStateHighlighted];
declare your button and make property of your button in .h file
UIButton *btnl;
#property(nonatomic,retain) IBOutlet UIButton *btnl;
and make connection from IB.
Now in .m file,
use this delegate method of you picker
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
btn.text=[yourArrayOfPickerContent objectAtIndex:row];
}
To change the custom button title use this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(82,203,129,45);
[button setTitle:#"btnTitle" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
To change button title,
button.titleLabel.text=#"String";
You should Code in the pickerView delegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
This way you can code about UIButton and can set the title to it.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0f, 4.0f, 90.0f, 20.0f)];
[btn setBackgroundColor:[UIColor clearColor]];
UIImage *backgroundView = [UIImage imageNamed:#"btn.png"];
[btn setBackgroundImage:backgroundView forState:UIControlStateNormal];
[btn setTitle:#"buttonName" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
first you need to create a pointer of type UIButton as IBOutlet in your view controller and than connect that button to the one in IB.
when you want to change the name of title just do
button.titleLabel.text = [NSString stringWithString:yourString];
I have something like this...
for(int i=0;i<10;i++){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button1" forState:UIControlStateNormal];
button.frame = CGRectMake(00.0, 00.0, 100.0, 30.0);
[view addSubview:button];
}
It's possible to instantiate each button with his own id? For example button+i ?
Thanks in advance!
You can set a numeric id to each button using the tag property:
button.tag = i;
You can then get the button instance afterwards with the code
[view viewWithTag:i];
provided you have added it to a view, like using [view addSubview:button].
I'm using 10 buttons in my interface and need, from time to time, to change the button's selector.
Am I required to use:
-(void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
before I change the selector or can I just use:
-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
I'm concerned that if I change the selector using the addTarget: method sans the removeTarget: method that I'll essentially "stack up" selectors for my UIButton to fire when it is pressed.
Yes you should always remove the previously add target before assigning the new target to the button. Like this---
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50, 50, 200, 50)];
[btn setTag:101];
[btn addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
btn = (UIButton *)[self.view viewWithTag:101];
[btn removeTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchUpInside];
now if you do this
btn = (UIButton *)[self.view viewWithTag:101];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchUpInside];
then both the methods method1 and method2 will be called.
Hope this helps.
Yes, you will need to remove the old target/action or both the old and new actions will be performed.