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.
Related
In my app,I am playing video using MPMoviePlayer.
I am adding a "Skip Ad" (you can see that on the right side of movie player)button on Movie Player.It works fine in iOS5, but when I run my app in iOS6, the button is hidden when Movieplayer becomes fullscreen.
Why this happens..any idea ?
Help me to overcome this..thanks in advance
Here are the code for adding button on MPMovieplayer,and the screen shots
UIButton *skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
skipButton.frame = CGRectMake(264.0,195.0, 67.0, 25.0);
[skipButton setTitle:#"" forState:UIControlStateNormal];
skipButton.backgroundColor = [UIColor clearColor];
[skipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal3 = [UIImage imageNamed:#"skipad.png"];
UIImage *strechableButtonImageNormal3 = [buttonImageNormal3 stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[skipButton setBackgroundImage:strechableButtonImageNormal3 forState:UIControlStateNormal];
UIImage *buttonImagePressed3 = [UIImage imageNamed:#"skipad.png"];
UIImage *strechableButtonImagePressed3 = [buttonImagePressed3 stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[skipButton setBackgroundImage:strechableButtonImagePressed3 forState:UIControlStateHighlighted];
[skipButton addTarget:self action:#selector(skipAction:) forControlEvents:UIControlEventTouchUpInside];
[[[UIApplication sharedApplication] keyWindow] addSubview:skipButton];
in iOS5 -
in iOS6 -
bringSubviewToFront should work, just make sure you're using it correctly. The usage is:
[skipButton.superview bringSubviewToFront:skipButton];
If that doesn't work you can change the zPosition like so:
skipButton.layer.zPosition = 1024.0;
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];
I am developing application which is using many photos so definitely it crashes though i released the images as some part is shown in following code
-(void)addScrollView{
[self selectData];
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(5, 0, 300, 160)];
int counter=10;
float y=10.0f;
int fullLength=[photoArray count];
int horizontal=(fullLength/2)*80;
int vertical=160;
int c1=1;
for(int c=0;c<[photoArray count];c++){
PhotoData *d=[photoArray objectAtIndex:c];
//NSLog(d.photoPath);
if(c==fullLength/2 &&c1<3){
counter=10;
y=y+80.0f;
c1++;
}
UIImage *img1=[[UIImage alloc]initWithContentsOfFile:d.photoPath];
UIButton* button = [[UIButton alloc] init];
[button setTitle:#"Delete" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
button.tag=d.photoId;
//set the button states you want the image to show up for
[button setBackgroundImage:img1 forState:UIControlStateNormal];
[button setFrame:CGRectMake(counter, y, 70.0, 70.0)];
//create the touch event target, i am calling the 'productImagePressed' method
[button addTarget:self action:#selector(deleteImage:)
forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
counter=counter+80.0f;
[img1 release];
[button release];
}
[scrollView setContentSize:CGSizeMake(horizontal, vertical)];
[self.view addSubview:scrollView];
[scrollView release];
}
Dealloc is also not called because i am using tabbased application.
So please help how can solve this memory management issue.
The only issue is what you mentioned—that you might have too many images or objects loaded. In that case, you may want to save some data to a file behind the scenes, and do fetches as needed. Maybe instead of storing all the objects in photoArray, write to a file (you could even just write the file path to a file) and batch load ten or so at a time, rather than all of them. Without more info though, there's not much more I can say.
I have written this code to see different image states...
UIButton *btnComment = [UIButton buttonWithType:UIButtonTypeCustom];
btnComment.tag=indexPath.row;
[btnComment addTarget:self action:#selector(goToComment:)forControlEvents:UIControlEventTouchDown];
UIImage *img1 = [UIImage imageNamed:#"commentbtndown.png"];
UIImage *img2 = [UIImage imageNamed:#"commentbtnup.png"];
UIImage *img3 = [UIImage imageNamed:#"commentbtnover.png"];
[btnComment setImage:img1 forState:UIControlStateNormal];
[btnComment setImage:img2 forState:UIControlStateHighlighted];
[btnComment setImage:img3 forState:UIControlStateSelected];
[btnComment setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];
btnComment.frame =CGRectMake(0, 100, 95, 25);
[cell addSubview:btnComment];
[img1 release];
[img2 release];
[img3 release];
but its not working, it is always showing me image 1.
p.s. I have added these images in the table view cell
The problem is that you are creating the UIImage objects with an autorelease method imageNamed, and you are releasing these objects afterwards, which cause your button to have invalid objects and because of that the images will not be displayed
Try removing this lines of code and your button will work
[img1 release];
[img2 release];
[img3 release];
And also, if you want the button to receive the touch events, you will have to add it to the contentView of your cell object, otherwise the button will be shown but you will not be able to tap it.
[cell.contentView addSubview:btnComment]
Well one problem with your code is that you should not be releasing those image variables. imageNamed: returns an autoreleased UIImage. I'm doubt that this is causing your problem, though.
Try using | instead of + for your fourth setImage call.
Another problem with your code is where you add the button to your UITableViewCell. Instead of this:
[cell addSubview:btnComment];
You should add subviews to your cell's contentView:
[cell.contentView addSubview:btnComment];
But I'm also not certain that this may cause your problem...
I have a app that has multiple buttons that are all assigned images in the interface builder. I then change there images during the application. What i want to know how would you is to change the images back to the original image....I did it by the following code but it keeps crashing the app
- (IBAction)resetCards
{
NSString *cardImage = [[NSString alloc] initWithFormat:#"CardBackground.png"];
UIImage *buttonImage = [UIImage imageNamed:cardImage];
[holdCardOne setImage:buttonImage forState:UIControlStateNormal];
[holdCardTwo setImage:buttonImage forState:UIControlStateNormal];
[flopCardOne setImage:buttonImage forState:UIControlStateNormal];
[flopCardTwo setImage:buttonImage forState:UIControlStateNormal];
[flopCardThree setImage:buttonImage forState:UIControlStateNormal];
[turnCard setImage:buttonImage forState:UIControlStateNormal];
[riverCard setImage:buttonImage forState:UIControlStateNormal];
[cardImage release];
[buttonImage release];
}
Don't call [buttonImage release] -- it is being autoreleased.
Usually only call release if you call alloc, copy, or retain. Always check the documentation for a message that returns an object if you are unsure, but normal Objective-C process is to return objects that autorelease.
In addition to Lou's answer, why not use:
UIImage *buttonImage = [UIImage imageNamed:#"cardBackground.png"];
Instead of creating an NSString?