Make UIBarButtonItem open a link in safari? - iphone

Hello I'm all new to the iPhone development thingy, so I would appreciate some help here.
I would like my UIBarButton to close the app and then open up a link in safari.
in the .h file
#import <UIKit/UIKit.h>
#interface MoviesViewController : UIViewController {
IBOutlet UIBarButtonItem *rightButton_;
}
- (void) goSafari;
#end
in the .m file
#import "MoviesViewController.h"
#implementation MoviesViewController
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
rightButton_ = [[[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStyleBordered target:self action:#selector(goSafari)]autorelease];
}
- (void) goSafari {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end

Your "gosafari" method implementation is incorrectly titled - should be "goSafari" to match the specified selector and method prototype.

Related

Navigating to another view using the UINavigationController

I'm using the UINavigationController in my app and for some reason it won't allow me to switch pages to the third xib called CompanyView. It switches fine from first to second but not second third. I'm probably doing something wrong but if someone could look over my code that would be great. I've got the button set correctly I believe.
Here is my .h file for the xib that won't change view:
#import <UIKit/UIKit.h>
#import "CompanyView.h"
#interface MenuView : UIViewController
-(IBAction)btnClicked:(id)sender;
#end
Here is my code for the .m file:
#import "MenuView.h"
#implementation MenuView
-(IBAction)btnClicked:(id)sender {
CompanyView * companyView = [[CompanyView alloc] init];
companyView.title = #"Company";
[self.navigationController pushViewController:companyView animated:YES];
[companyView release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Try initializing your Company View this way.
CompanyView *companyView = [[CompanyView alloc] initWithNibName:#"CompanyView" bundle:nil];
this is how i set it up:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CategoryPresetViewController *controller = [[CategoryPresetViewController alloc] initWithPVCDelegate:avc];
controller.title = [[factoryCategoryNameArray objectAtIndex:indexPath.row] retain];
if (controller != nil){
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
maybe it can help you

shouldStartLoadwithRequest not getting called in subclass

I have a view controlled app. On my xib I have no webviews but I do have buttons that bring up classes that have webviews. So hit button one and a uiwebview pops up so on and so forth.
Now in one of my classes I pull up a remote webpage that has a link on it. I want to over-ride that button link with shouldStartLoadwithrequest. How ever this never seems to get called.
Right now my class is called Tenth.m and I have this code in my view controller
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#" Test spot 1");
NSString* scheme = [[request URL] scheme];
if ([#"didTap" isEqual:scheme]) {
// Call your method
NSLog(#" Test spot 2");
[self didTapButton1];
return NO;
} else {
NSLog(#" Test spot 3");
return YES;
}}
But I get nothing (none of my NSLogs). I have tried putting this into my Tenth.m class and still nothing. I have looked at other examples and even downloaded one but it only uses the viewcontroller and delegates classes. No third class.
I'm lost as to why its not getting called.
Here is my third class
#import "Tenth.h"
#implementation Tenth
- (void)awakeFromNib
{
[category10 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.mywebsite.com/api/didtap.php"]]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#end
remote page code
hello
Tenth.h file
#import <UIKit/UIKit.h>
#interface Tenth : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *category10;
}
-(IBAction)back;
#end
My first guess is that the Web view does not have its delegate set. In order for it to call
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
The class containing this method needs to be set as the webview's delegate. So somewhere in the code there should be a line similar to webView.delegate = <variable>, where the variable is the instance of the class which has implemented the shouldStartLoadWithRequest: method.
This is often "self" when the webView is a subview of a viewController's view. But the way you have described your application, I am not sure if it will be self or not, but it sounds like it will not be self, but the name of the instance of a different class.
Maybe try this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Remember to set web view delegate to self.
category10.delegate = self;
}

Keeping UIButton highlighted

Hey!
I have a xib file were i would like a round rect button to stay highlighted after it is pressed. I also would like to have a different button that is pressed after the first one that takes you to the next page. How can I do this. Some code would be greatly appreciated!
This is my .h:
#import <UIKit/UIKit.h>
#interface Test1ViewController : UIViewController {
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
}
-(IBAction) buttonPressed:(id)sender;
-(IBAction) secondButtonPressed:(id)sender;
- (void)flipButton;
#end
This is my .m:
#import "Test1ViewController.h"
#import "Page2.h"
#implementation Test1ViewController
-(IBAction)buttonPressed:(id)sender
{
[self performSelector:#selector(flipButton) withObject:nil afterDelay:0.0];
}
- (IBAction)secondButtonPressed {
if ( button1.selected ) {
Page2 *page2 = [[Page2 alloc] initWithNibName:#"Page2" bundle:nil];
page2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:page2 animated:YES];
[page2 release];
}
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)flipButton {
if ( button1.selected ) {
button1.highlighted = NO;
button1.selected = NO;
} else {
button2.highlighted = YES;
button2.selected = YES;
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Thanks a lot for all the help!
You will have to use the highlighted property of the button to set the state as highlighted or otherwise. However doing immediately on Touch Up Inside seems to reset it. So we delay the change until the next run loop starts. Do this on the method called on touch.
-(IBAction)buttonPressed:(id)sender
{
[self performSelector:#selector(flipButton) withObject:nil afterDelay:0.0];
}
and define the flip method as follows –
- (void)flipButton {
if ( self.button.selected ) {
self.button.highlighted = NO;
self.button.selected = NO;
} else {
self.button.highlighted = YES;
self.button.selected = YES;
}
}
You can later check on the method called on tap of the other button whether self.button.selected is YES or not and then act on it.
- (IBAction)secondButtonPressed {
if ( self.button.selected ) {
// Load next page.
}
}
Better Approach
Use a UISwitch. Don't you think that it is a natural fit.

Odd Scrolling Issue ( UIScrollView )

I have searched endlessly to no avail on this, hopefully someone can help!
I have a UIScrollView on the left half of a UIView which loads in custom cells/subviews. The UIView is part of a UINavigation stack, and it's also loaded into a tab on a TabBar.
What happens is, if I start the app and begin scrolling right away it's very smooth. However if I start the app and wait 5-10 seconds, the UIScrollView is VERY laggy and choppy (and it stays like that). I would think that it would be a memory leak or something, but I can't seem to find anything.
Included is the code of the view where I'm loading the custom cells/subviews into the UIScrollView. There isn't any custom code in the cell subview. Oh, and there's only about 8-10 items: each item has a small (150x150) image, and 3 text fields - all opaque.
#import "ProductListViewController.h"
#import "ProductListLeftItemViewController.h"
#implementation ProductListViewController
#synthesize listScroll;
- (void)viewDidLoad {
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:#"Products" ofType:#"plist"];
NSArray *products = [[NSArray alloc] initWithContentsOfFile:path];
//
int numProducts;
numProducts = [products count];
[listScroll setContentSize:CGSizeMake(500, (numProducts * 111))];
for (int i = 0; i < numProducts; i++) {
ProductListLeftItemViewController *cellItem = [[ProductListLeftItemViewController alloc] initWithNibName:#"ProductListLeftItem" bundle:nil];
cellItem.view.frame = CGRectMake(0, (i*111), 500, 111);
[self.listScroll addSubview:cellItem.view];
cellItem = nil;
[cellItem release];
}
[products release];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
NSLog(#"Memory warning, ProductListViewController");
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[listScroll release];
[super dealloc];
}
#end
And as requested, the ProductListLeftItemViewController code:
#import "ProductListLeftItemViewController.h"
#implementation ProductListLeftItemViewController
#synthesize titleTxt, descriptionTxt, modelTxt, productThumb;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
titleTxt = nil;
descriptionTxt = nil;
modelTxt = nil;
productThumb = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[titleTxt release];
[descriptionTxt release];
[modelTxt release];
[productThumb release];
[super dealloc];
}
#end
For anyone that has this problem in the future, once I started testing things on the actual device, everything worked perfectly smooth. Maybe it was just something quirky with the simulator? I'll consider this resolved, even though I'm still going to look into it a little further.

PhoneGap Iphone Apps, Touch pad contains additional Bar

I have created a i phone app using phoneGAP.When i tested my app in my ipod, there is a additional bar found at the top of touch pad(it contains buttons like Done,previous,next).But it is not found on the apps that created using objective C.Anybody know how i removed this bar.
Thanks,
The form assistant (prev, next, done) bar can be removed by using a somewhat hacky but working solution. This of course assumes you use phonegap. It simply cannot be done in a regular web app / web page.
Replace the contents of your MainViewController.m with this:
#import "MainViewController.h"
#implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
- (void) removeBar {
// Locate non-UIWindow.
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIWebFormView.
for (UIView *possibleFormView in [keyboardWindow subviews]) {
// iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
if ([[possibleFormView description] rangeOfString:#"UIPeripheralHostView"].location != NSNotFound) {
for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
if ([[subviewWhichIsPossibleFormView description] rangeOfString:#"UIWebFormAccessory"].location != NSNotFound) {
[subviewWhichIsPossibleFormView removeFromSuperview];
}
}
}
}
}
- (void)keyboardWillShow:(NSNotification*) notification {
// remove the bar in the next runloop (not actually created at this point)
[self performSelector:#selector(removeBar) withObject:nil afterDelay:0];
}
#end