iphone - 将后退按钮添加到 View Controller

标签 iphone ios objective-c

我已经为 3 个 UIToolBar 项目创建了 3 个 UIViewControllers,但我无法在每个 Controller 上创建一个按钮来返回主页或主视图 Controller 。请帮助我,因为我是 objective-c 的新手。

这是我的代码......

“ViewController.h”

#import "ViewController.h"
#import "ContactInfoViewController.h"
#import "DateViewController.h"
#import "MessageViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 417, self.view.frame.size.width, 44);
UIBarButtonItem *contact = [[UIBarButtonItem alloc] initWithTitle:@"Contact" style:UIBarButtonItemStyleDone target:self action:@selector(ContactButton)];
UIBarButtonItem *flexiableItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *date = [[UIBarButtonItem alloc] initWithTitle:@"Date" style:UIBarButtonItemStyleDone target:self action:@selector(DateButton)];
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *message = [[UIBarButtonItem alloc] initWithTitle:@"Message" style:UIBarButtonItemStyleDone target:self action:@selector(MessageButton)];
NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:contact,flexiableItem1,date,flexiableItem,message, nil];
[toolbar setItems:items animated:YES];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
-(void)ContactButton
{
ContactInfoViewController *secondViewcont = [[ContactInfoViewController alloc] init];
UINavigationController *navigCont = [[UINavigationController alloc] initWithRootViewController:secondViewcont];
[self presentViewController:navigCont animated:YES completion:nil];
}
-(void)DateButton
{
DateViewController *secondViewcont = [[DateViewController alloc] init];
UINavigationController *navigCont = [[UINavigationController alloc] initWithRootViewController:secondViewcont];
[self presentViewController:navigCont animated:YES completion:nil];
}
-(void)MessageButton
{
MessageViewController *secondViewcont = [[MessageViewController alloc] init];
UINavigationController *navigCont = [[UINavigationController alloc]  initWithRootViewController:secondViewcont];
[self presentViewController:navigCont animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end

最佳答案

将自定义按钮添加到第二个 View Controller

viewDidLoad中你可以这样做

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)]autorelease];


- (IBAction)doneButtonPressed:(id)sender 
{
   [self dismissViewControllerAnimated:YES completion:nil];
}

关于iphone - 将后退按钮添加到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14722117/

相关文章:

iphone - 保存 UIImage,以错误的方向加载它

iphone - AFNetworking+UIImage 先下载低分辨率图片

iOS 自动布局 : Display the cell only after its contents are updated

ios - 使用用户定义的单元格以编程方式创建 UITableViewController

ios - 在 View 接收到手势时做某事

iphone - 如何在 iphone 中检测麦克风的打击而不是语音?

ios - 核心数据 - 复杂获取

iOS DeviceToken : How to pass deviceToken from AppDelegate to ViewController?

ios - 当你的应用程序曾经有watchkit时,你可以向苹果提交不带watchkit的应用程序吗

ios - 如何创建固定数量的 UITableViewCell,如果数据为空,则不应显示行