2011-08-18 52 views
0

你好我都是iPhone新手,所以我很感謝這裏的幫助。 我想我的UIBarButton關閉應用程序,然後在safari中打開一個鏈接。讓UIBarButtonItem在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 
+1

你有什麼問題? – jtbandes

+0

這裏的代碼沒有錯。而且你正在代碼中設置目標/動作,所以它對於IB連接也不是問題。 – FeifanZ

+0

當我點擊按鈕什麼都沒有...... –

回答

2

你 「gosafari」 方法的實現是不正確標題 - 應該是 「goSafari」 以匹配指定的選擇和方法的原型。

+0

我改變了它,但它仍然沒有工作... –

+0

在goSafari中放置了一個斷點 - 它會被調用嗎?你的「不完整的課堂實施」警告是否消失? – TomSwift

+0

也從您的代碼中不清楚「rightButton_」正在被使用。看起來你有另一個按鈕「donateButton_」,它可以通過Interface Builder在你的XIB中設置。如果是這種情況,請仔細檢查按鈕事件處理程序是否在Interface Builder中正確連接。 – TomSwift

相關問題