OStack程序员社区-中国程序员成长平台

标题: iphone - 将后退按钮添加到 View Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 00:49
标题: iphone - 将后退按钮添加到 View Controller

我已经为我的 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 actionselector(ContactButton)];
UIBarButtonItem *flexiableItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *date = [[UIBarButtonItem alloc] initWithTitle"Date" style:UIBarButtonItemStyleDone target:self actionselector(DateButton)];
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *message = [[UIBarButtonItem alloc] initWithTitle"Message" style:UIBarButtonItemStyleDone target:self actionselector(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



Best Answer-推荐答案


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

viewDidLoad 你可以这样做

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self actionselector(doneButtonPressed]autorelease];


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

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4