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];
Related
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)
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.
well, i'm making an app from a UiWebView example i found. and i needed to add a button but the way this example works is similar to phonegap so i figured out i need to add the button as a subview to the window, so then i got my button and set it up for pressing but when ever i press it my app crashes... can any one help? here is my code snippet:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
myLoadingLabel.hidden = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[window addSubview:webView];
//my button
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
playButton.frame = CGRectMake(122, 394, 76, 76);
[playButton setTitle:#"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"mic.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"mic.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[playButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:playButton];
}
-(void)playAction {
NSLog(#"Button Pressed!");
}
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIWebView_ExampleAppDelegate playAction:]:
oh and i know its a long shot, but i need this button outside of the webpage because it needs to start recording audio when clicked, then when clicked again it needs to stop recording and save, run a command with system();, and then put the data gotten from the system command into the uiwebview to be used... so yea if anyone knows some code i would appreciate it greatly, and its a jailbreak app so the system command will be ok. Thanks!!!
You are invoking a method named playAction: but you implement a method named playAction. Mind the missing colon. The crash most likely is related to that issue.
For a quick fix, change
[playButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
to
[playButton addTarget:self action:#selector(playAction) forControlEvents:UIControlEventTouchUpInside];
The problem is this:
#selector(playAction:)
vs.
- (void)playAction
The former refers to a method called -playAction: that takes one parameter, but you’ve implemented a method called -playAction that takes no parameters. Drop the colon from the #selector block and it should work.
I create a few UIBarButtonItem with interface builder and try to customize the button to green color. While I couldn't find ways to change background color of the button, I decide to code in this way:
IBOutlet UIBarButtonItem *btnDone;
-(void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [[UIImage imageNamed:#"btnGreen.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[doneButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[doneButton addTarget:self action:#selector(saveDateEdit:) forControlEvents:UIControlEventTouchUpInside];
[doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
doneButton.frame = CGRectMake(0.0, 0.0, 50, 30);
btnDone = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
[doneButton release];
}
I create IBOutlet and link to the button from interface builder while I try to override this during it first load. However, it is showing the default button.
Would like to know why is this happening? Or even better someone can suggest me better way changing my UIBarButtonItem to green.
Appreciate your help!
You are missing one point,
You are de-referring the btnDone object- as first you assign it using IBOutlet and then de-reffer by using btnDone = [[UIBarButtonItem alloc] init...].. what you should do is add it programmatically instead of using IBOutlet. it will do your work.
Thanks,
How can I set the background color of a particular button in iPhone?
I used:
btnName1 = [ColorfulButton buttonWithType:UIButtonTypeRoundedRect];
btnName1.frame=CGRectMake(45,146,220,40);
[btnName1 setTitle:#"Continue" forState:UIControlStateNormal];
[btnName1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnName1 setImage:[UIImage imageNamed:#"green.png"] forState:UIControlStateNormal];
UIImage *img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"greenish" ofType:#"png"]];
UIImage *strechableImage = [img stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[btnName1 setBackgroundImage:strechableImage forState:UIControlStateNormal];
[btnName1 setNeedsDisplay];
[InfoView addSubview:btnName1];
It's correctly working on iPhone simulator but not on iPod (color not shown in iPod).
myButton.backgroundColor = [UIColor redColor];
Or whatever colour you want. This method is inherited from UIView.
Note that this only sets the background colour...
If you want to change the button's look:
[myButton setBackgroundImage:[UIImage imageNamed:#"MyImage.png"] forState:UIControlStateNormal];
You can change the state to whichever state you like.
EDIT: If you are adding the button from interface builder, make sure to change the button Type to Custom, and change the image.
This is how I got it done.
In the .h file declare yourButton as an IBOutlet.
#property (weak, nonatomic) IBOutlet UIButton *yourButton;
In the .m file where you want to change the colour of the button use either
[yourButton setBackgroundColor:[UIColor blueColor]];
or
[yourButton setBackgroundColor:[UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0]];
To obtain the colours I require I use this link.
i had the solution when code is written in viewdidload() it's properly work eariler i used for uiview thatswhy it's not work.