【问题标题】:How to add title and Done button on toolbar in a modal view如何在模态视图的工具栏上添加标题和完成按钮
【发布时间】:2011-09-26 16:36:06
【问题描述】:

我尝试从导航视图控制器 (DateViewController) 呈现模式视图 (InfoViewController)。

我在 InfoViewContoller 的视图顶部添加了一个工具栏。现在我想在工具栏上添加一个标题“信息”和一个“完成”按钮。(完成按钮将执行 infoDismissAction 方法)

谁能给我一些建议?非常感谢!

这里是 DateViewController.h 的代码

#import <UIKit/UIKit.h>
#import "InfoViewController.h"
@interface DateViewController : UIViewController 
{
    InfoViewController *infoViewController;
}
@property (nonatomic, retain) InfoViewController *infoViewController;
@end

DateViewController.m

- (IBAction)modalViewAction:(id)sender{    
    if (self.infoViewController == nil)
        self.infoViewController = [[[InfoViewController alloc] initWithNibName:
                                   NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
    [self presentModalViewController:self.infoViewController animated:YES];
}

- (void)dealloc{
    if (self.infoViewController != nil)
    {
        [infoViewController release];
    }
    [super dealloc];
}

- (void)viewDidLoad{
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Info"  style:UIBarButtonSystemItemPlay target:self  action:@selector(modalViewAction:)] autorelease];
    [modalBarButtonItem release];
    [super viewDidLoad];
}

这里是 InfoViewController.m

- (IBAction)infoDismissAction:(id)sender{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    UIToolbar *toolBar;
    toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    toolBar.frame = CGRectMake(0, 0, 320, 50);
    toolBar.barStyle = UIBarStyleDefault;
    [toolBar sizeToFit];        
    [self.view addSubview:toolBar]; 
    [toolBar release];
    [backButton release];
    [super viewDidLoad];
}

【问题讨论】:

    标签: iphone objective-c ios modalviewcontroller uitoolbar


    【解决方案1】:

    试试这个代码。

    - (void)viewDidLoad {
           UIToolbar *toolBar;
                toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
                toolBar.frame = CGRectMake(0, 0, 320, 50);
                toolBar.barStyle = UIBarStyleDefault;
                [toolBar sizeToFit];      
    UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
    UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] initWithTitle:@"INFO" style:UIBarButtonItemStyleBordered target:self action:@selector(InfoAction:)] autorelease];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"DONE" style:UIBarButtonItemStyleBordered target:self action:@selector(doneAction:)];
    
    
                NSArray *barButton  =   [[NSArray alloc] initWithObjects:flexibleSpace,infoButton,flexibleSpace,doneButton,nil];
                [toolBar setItems:barButton];
    
            [self.view addSubview:toolBar];
            [toolBar release];
            [barButton release];
            barButton = nil;
            [super viewDidLoad];
    
    }
    

    【讨论】:

    • 谢谢!现在我知道如何使用flexibleSpace了。但我的意思是如何添加“信息”标题而不是“信息”栏按钮。
    【解决方案2】:

    我建议使用您的 InfoViewController 设置另一个 UINavigationController 并将导航控制器呈现为您的模态视图。

    要回答您的问题,您需要像这样填写您的 UIToolbar:

    - (void)viewDidLoad {
      [super viewDidLoad];
      UIToolbar *toolBar;
      toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
      toolBar.frame = CGRectMake(0, 0, 320, 50);
      toolBar.barStyle = UIBarStyleDefault;
      [toolBar sizeToFit];        
      [self.view addSubview:toolBar]; 
      [toolBar release];
    
      UIBarButtonItem* bbiInfo = [[UIBarButtonItem alloc] initWithTitle:@"Info" style:UIBarButtonItemStyleBordered target:self action:@selector(tappedInfoButton)];
      UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
      UIBarButtonItem* bbiDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(tappedDoneButton)];
      NSArray* items = [[NSArray alloc] initWithObjects:bbiInfo, flexibleSpace, bbiDone, nil];
      [toolBar setItems:items];
    
      [items release];
      [bbiInfo release];
      [flexibleSpace release];
      [bbiDone release];
    }
    

    【讨论】:

    • 谢谢!现在我知道如何使用flexibleSpace了。但我的意思是如何添加“信息”标题而不是“信息”栏按钮。
    • UIBarButtonItem* infoTitle = [[UIBarButtonItem alloc] initWithTitle:@"Info" style:UIBarButtonItemStylePlain target:self action:nil];
    • 谢谢:P 但是有什么办法可以把 infoTitle 放在 toolBar 的中心呢?我尝试了以下代码,但无法将 infoLabel 置于中心位置。 NSArray* items = [[NSArray alloc] initWithObjects:flexibleSpace,infoLabel, flexibleSpace, bbiDone, nil]; [工具栏 setItems:items];
    • 这是我尽量避免使用 UIToolbar 的原因之一。 UINavigationBar 为您处理。如果您坚持使用 UIToolbar,我建议您使用 UIBarButtonSystemItemFixedSpace 并根据自己的喜好调整其宽度。
    • 我的同事@slaunchman 更喜欢这种方法,其原因将在未来变得清晰。
    猜你喜欢
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多