Webview won't go back to original page - iphone

Basically I am trying to call the back button to goto the previous web page, how ever it seems it isn't able to do so. I am not sure if I am missing a minor detail. This seems to work when I am on the original page click on a link then another link then push back button to get to the first link, but won't go back to original page(hope that make sense).
- (void)viewDidLoad {
[super viewDidLoad];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 119, self.view.bounds.size.width, self.view.bounds.size.height - 165)];
webView.backgroundColor = [UIColor whiteColor];
webView.userInteractionEnabled = NO;
webView.opaque = NO;
[webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];
[self.view addSubview:webView];
webView.delegate = self;
webView.scalesPageToFit = YES;
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 34, 34);
[backButton setImage:[UIImage imageNamed:#"back_arrow"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
}
- (void)back {
[webView goBack];
}

If You do not give UIWebViewDelegate in .h file then first give it & then if your problem is not solve then simply comment this line & then check:
webView.userInteractionEnabled = NO;
I run your code everything is fine but I run your code with comment that line.

Related

how to make a pause menu for iphone game?

I was wondering which method is the easiest or "smartest" way of making a pause menu for games?
I don´t have that much experience with creating game pause menus so I´ve tried 2 methods that I came up with by my own. Both or in code beneath this text.
The first method I tried was to put a black almost transparent filter on top of the game, that I paused. I was successful with pausing everything except the touches made underneath the filter on my uiimages. I thought that putting a new uiimage on top of the current one would disable the touches made on the uiimages underneath it. I tried as well with touchenabled = NO but still nothing.
-(IBAction)pause{
NSLog(#"pause");
[countdowntimer invalidate];
[self.rubin1 stopAnimating];
[self.rubin2 stopAnimating];
[self.rubin3 stopAnimating];
Pause.enabled = NO;
music1.enabled = NO;
music2.enabled = NO;
sfx1.enabled = NO;
sfx2.enabled = NO;
//PauseMenu background
UIImage *image1 = [UIImage imageNamed: #"filter2.png"];
pausefilter = [[[UIImageView alloc] initWithImage:image1] autorelease];
CGRect newFrame1 = pausefilter.frame;
newFrame1 = CGRectMake(0,0, image1.size.width, image1.size.height);
pausefilter.frame = newFrame1;
[self.view addSubview:pausefilter];
[image1 release];
//Resume to Game
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(90, 146, 80, 18);
[button1 setImage:[UIImage imageNamed:#"RESUME.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(resume) forControlEvents:UIControlEventTouchUpInside];
[ self.view addSubview: button1 ];
//End Game
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(260, 140, 180, 30);
[button2 setImage:[UIImage imageNamed:#"EndGame.png"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(end) forControlEvents:UIControlEventTouchUpInside];
[ self.view addSubview: button2 ];
[sharedSoundManager pausePlayingMusic];
}
The second method I tried was to go back (switch view) to the options on the main menu to use the settings from there instead but I wasn´t able to resume back to where I paused the game. Instead the game restarted from the beginning.
-(IBAction)pause{
NSLog(#"pause");
[countdowntimer invalidate];
[self.rubin1 stopAnimating];
[self.rubin2 stopAnimating];
[self.rubin3 stopAnimating];
Pause.enabled = NO;
music1.enabled = NO;
music2.enabled = NO;
sfx1.enabled = NO;
sfx2.enabled = NO;
menureturn = [[MainMenu alloc] initWithNibName:#"MainMenu" bundle: nil];
menureturn.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:menureturn animated: YES];
self.menureturn.Start.hidden = YES;
self.menureturn.option.hidden = YES;
self.menureturn.OptionsHeadtitle.hidden = NO;
self.menureturn.HowToPlayHeadtitle.hidden = YES;
self.menureturn.howtoplay.hidden = YES;
self.menureturn.filter.hidden = NO;
self.menureturn.music1.hidden = NO;
self.menureturn.music2.hidden = YES;
self.menureturn.SFXimage.hidden = NO;
self.menureturn.Musicimage.hidden = NO;
self.menureturn.sfx1.hidden = NO;
self.menureturn.sfx2.hidden = YES;
self.menureturn.credits.hidden = YES;
self.menureturn.back.hidden = YES;
self.menureturn.creditsinfo.hidden = YES;
self.menureturn.resume.hidden = NO;
self.menureturn.endgame.hidden = NO;
[sharedSoundManager pausePlayingMusic];
}
Is there a third option of how to make a god pause menu or just redo one of the two I already tried?
You can have a BOOL variable like
BOOL isGamePaused
Now when you are playing game i.e running not paused assign
isGamePaused = NO;
While you pause
isGamePaused = YES;
Now while playing animation or something like that you can use condition like
if(!isGamePaused) { ProceedAnimation;}
Hope this helps
The best thing to disable any event is to create a new element in your pause menu which will be the first responder and catches all events. So nothing can arrive to your game's interface ;)

Can't get programmatically made UIButton to work - iPhone

Here is the code I'm using:
-(void)viewDidLoad
{
NSString *checkerPath = [[NSBundle mainBundle] pathForResource:#"black-yellow-checker" ofType:#"png"];
UIImage *checkerImage = [[UIImage alloc] initWithContentsOfFile:checkerPath];
checkerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 230.0f, 320.0f, 280.0f)];
[checkerView setImage:checkerImage];
UIButton *backButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
backButton.frame = CGRectMake(45.0f, 175.0f, 230.0f, 50.0f);
[backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"yellowButton" ofType:#"png"]] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(goBackHome:) forControlEvents:UIControlEventTouchDown];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont fontWithName:#"Marker Felt" size:30];
backButton.titleLabel.textColor = [UIColor blackColor];
[checkerView addSubview:backButton];
}
- (void)goBackHome:(id)sender
{
NSLog(#"go back pressed");
}
Not sure why this doesn't work. The button shows up, but when I press it nothing happens. It doesn't even change to the "indented" image when I touch it. Nothing. Can't use IB, has to be programmatically. Any thoughts?
UIImageView has userInteractionEnabled set to NO by default. This prevents any subviews from getting touches.
You could either enable user interaction of the image view by
checkerView.userInteractionEnabled = YES;
or create a new UIView to include both checkerView and the backButton.
UIView* newView = ...
...
[newView addSubview:checkerView];
[newView addSubview:backButton];
I believe that the forControlEvents: has to be UIControlEventTouchUpInside.

Custom back button click event on pushed view controller

I have pushed view controller and load WebView and Custom rectangular rounded button on right down left corner into view using programmatic way.
-(void)loadView {
CGRect frame = CGRectMake(0.0, 0.0, 480, 320);
WebView = [[[UIWebView alloc] initWithFrame:frame] autorelease];
WebView.backgroundColor = [UIColor whiteColor];
WebView.scalesPageToFit = YES;
WebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin);
WebView.autoresizesSubviews = YES;
WebView.exclusiveTouch = YES;
WebView.clearsContextBeforeDrawing = YES;
self.roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
self.roundedButtonType.frame = CGRectMake(416.0, 270.0, 44, 19);
[self.roundedButtonType setTitle:#"Back" forState:UIControlStateNormal];
self.roundedButtonType.backgroundColor = [UIColor grayColor];
[self.roundedButtonType addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
self.view = WebView;
[self.view addSubview: self.roundedButtonType ];
[WebView release];
}
This is action that I have added as back button of navigation.
-(void)back:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)viewDidUnload{
self.WebView = nil;
self.roundedButtonType = nil;
}
-(void)dealloc{
[roundedButtonType release];
[super dealloc];
}
Here, When Back button click then it is showing previous view but application got stuck in that view and GDB shows Program received signal :EXC_BAD_ACCESS message.
how resolve this issue?
Thanks,
You have -autorelease'd WebView here:
WebView = [[[UIWebView alloc] initWithFrame:frame] autorelease];
but then you -release it again!
[self.view addSubview: self.roundedButtonType ];
[WebView release];
Try to remove one of the releases.
Also,
self.roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
unless roundedButtonType is declared as #property(assign,...), you don't need to send the -retain message. And it's better to setup the frame, title, etc. before assigning to self.roundedButtonType, because every call to self.roundedButtonType is not free.

UIButton not being clicked in iPhone programming

I've created a UIButton on a view and on action of the UIButton[UIButton* button1_obj] one more button[UIButton* button2_obj] is created on same view.
i'm facing these 2 issues...please guide me how to proceed.
button1_obj is appearing when i run my project, but its not being clicked or highlighted, nor any exception is thrown.
On action of button1_obj , button2-obj has to appear on the same view, how to clear the view before placing button2_obj on it.
Code for question (1):
-(void)start
{
NSLog(#"--------------------------------------------------------------------");
NSLog(#"Entered CView start");
//define size and position of view
subMainView_G_obj = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480, 320)]; //initilize the view
//subMainView_G_obj.autoresizesSubviews = YES; //allow it to tweak size of elements in view
UIButton_G_obj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
UIButton_G_obj.frame = CGRectMake(100,70, 150, 50);
UIButton_G_obj.backgroundColor = [UIColor clearColor];
//UIButton_G_obj.adjustsImageWhenHighlighted = YES;
[UIButton_G_obj setTitle:#"UI" forState:UIControlStateNormal];
[UIButton_G_obj addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[subMainView_G_obj addSubview:UIButton_G_obj];
UIButton_G_obj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
UIButton_G_obj.frame = CGRectMake(100,150, 150, 50);
UIButton_G_obj.backgroundColor = [UIColor clearColor];
UIButton_G_obj.adjustsImageWhenHighlighted = YES;
[UIButton_G_obj setTitle:#"Configuration" forState:UIControlStateNormal];
[UIButton_G_obj addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[subMainView_G_obj addSubview:UIButton_G_obj];
NSLog(#"Leaving CView start");
NSLog(#"--------------------------------------------------------------------");
}
- (void)action:(id) sender
{
NSLog(#"Inside action method.. On click of First View");
buttonUIObj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
buttonUIObj.frame = CGRectMake(100,160,100,50);
[buttonUIObj setTitle:#"Next View" forState:UIControlStateNormal];
buttonUIObj.backgroundColor = [UIColor clearColor];
[subMainView_G_obj addSubview:buttonUIObj];
}
i've declare UIButton_G_obj,buttonUIObj,subMainView_G_obj GLOBALLY.
Hard to know what the problem with button1_obj is, without seeing code. However, for (2), you will probably want to remove button1_obj by calling [button1_obj removeFromSuperview].
About problem 1:
First of all both your buttons are named the same. Therefore you have a memory leak because your are allocating space for your button twice. Use different names for your buttons.
About problem 2:
You could hide button 2 until button 1 is pressed: In -(void)start
UIButton_G_obj_1.tag = 1;
...
UIButton_G_obj_2.hidden = YES;
UIButton_G_obj_2.tag = 2;
And in the action method you can unhide the button:
if (sender.tag == 1) {
UIButton_G_obj_2.hidden = NO;
}

How to add a UIButton at runtime

I am trying to add a UIButton at runtime however it is not visible. What am I doing wrong?
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(0, 0, 100, 25);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick:)
forControlEvents:UIControlEventTouchUpInside];
btn.center = self.center;
[self addSubview:btn];
}
return self;
}
First, make sure the initWithFrame: method is being called. If your view is in a Nib, initWithCoder: is being called instead.
Second, is the button the only subview (from your code it looks like it is, but you never know). The button could be hidden behind another subview. Call bringSubviewToFront: if you need to.
Finally, is the view itself visible? Is it big enough to show the button? Given your example, if the view is less than 100 pixels wide, the button won't show because it will get clipped by the view's bounds.
You must release btn and remove ":" in buttonClick:
UIButton *btn= [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(0, 0, 100, 25);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
if it still doesn't work, try removing the : at the end of the selector name: #selector(buttonClick)
You don't need to retain the UIButton because it is retained by [self.view addSubview:btn];
First check whether your code is executing the initwithFrame method.
Because if you are loading the view from nib i.e using
NSArray *xibviews = [[NSBundle mainBundle] loadNibNamed: #"MySubview" owner: mySubview options: NULL];
MySubview *msView = [xibviews objectAtIndex: 0];
[self.view addSubview:msView];
Then the initWithFrame part will not be executing.So please check once.