如何在模式视图中的工具栏上添加标题和完成按钮

发布于 2024-12-06 17:22:45 字数 1828 浏览 0 评论 0原文

我尝试从导航视图控制器(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];
}

I try to present a modal view (InfoViewController) from a navigation view controller(DateViewController).

I add a toolbar on top of InfoViewContoller's view. Now I want to add a title "Info" and a "Done" button on the toolbar.(The Done button will perform the infoDismissAction method)

Can anyone give me som tips? Thanks a lot!

Here's code of 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];
}

Here's 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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫思念不要吵 2024-12-13 17:22:45

试试这个代码。

- (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];

}

Try this code.

- (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];

}
ヅ她的身影、若隐若现 2024-12-13 17:22:45

我建议使用 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];
}

I'd recommend setting up another UINavigationController with your InfoViewController and present the navigation controller as your modal view.

To answer your question you'd want to fill in your UIToolbar like this:

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文