1

我正在开发适用于 iPad 和 iPhone 的应用程序,我在导航栏中添加了多个栏项目,但我想添加不同的栏项目...在 iPad 的导航栏中,我只想添加 2 个项目权利,这已经在起作用。但我还想在 iPhone 导航栏的左侧添加 2 个其他按钮。但是通过我尝试的方式,我得到了相同的导航栏,相同的项目,关于如何向导航栏添加不同项目的任何想法。

这是我的代码:

UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] 
  initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
  target:self action: @selector(IpadReload:)];

UIImage *emailIcon = [UIImage imageNamed:@"emailicon.png"];
UIBarButtonItem *emailButton = [[UIBarButtonItem alloc] initWithImage:(emailIcon) style:UIBarButtonItemStylePlain target:self action:@selector(emailButtonTapped:)];

NSArray *rightButtonsArray = [[NSArray alloc] initWithObjects:refreshButton, emailButton, nil];

self.navigationItem.rightBarButtonItems = rightButtonsArray;

UIBarButtonItem *previousButton = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStylePlain  target: self action: @selector(PreviousClicked:)];

UIImage *phoneIcon = [UIImage imageNamed:@"phoneicon.png"];
UIBarButtonItem *phoneButton = [[UIBarButtonItem alloc] initWithImage:(phoneIcon) style:UIBarButtonItemStylePlain target:self action:@selector(callPhone:)];

NSArray *leftButtonsArray = [[NSArray alloc] initWithObjects:previousButton, phoneButton, nil];

self.navigationItem.leftBarButtonItems = leftButtonsArray;
4

3 回答 3

2

实际上,您需要使用 iOS 内置的当前设备枚举,而不是 isEqualToString:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    // iPad
} else {
    //  iPhone or iPod Touch - Add your two items to the right here
}
于 2012-06-03T11:46:18.750 回答
1

区分 iPhone 和 iPad 的简洁方法是:

BOOL iPad = [[UIDevice currentDevice] userInterfaceIdiom] == 
    UIUserInterfaceIdiomPad;
// your setup
UIImage *buttonIcon = [UIImage imageNamed: 
   iPad ? @"padIcon.png" : @"phoneIcon.png"];
于 2012-06-03T11:47:46.563 回答
0

首先区分iPhone和iPad,根据它添加导航栏项 NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone"]){
//Add iPhone navigation items
}else if ([deviceType isEqualToString:@"iPhone"]){
//Add iPad navigation items
}else{

//ipod 等的默认项目 }

于 2012-06-03T11:16:18.097 回答