GVKun编程网logo

如何向 UINavigationController 添加右键?(添加ui界面)

11

在这篇文章中,我们将为您详细介绍如何向UINavigationController添加右键?的内容,并且讨论关于添加ui界面的相关问题。此外,我们还会涉及一些关于DestinationViewCont

在这篇文章中,我们将为您详细介绍如何向 UINavigationController 添加右键?的内容,并且讨论关于添加ui界面的相关问题。此外,我们还会涉及一些关于DestinationViewController Segue和UINavigationController迅速、Implementing Navigation with UINavigationControlle、ios – UINavigationBar -pushNavigationItem在将新控制器推送到UINavigationController堆栈时从不调用、ios – UINavigationBar没有UINavigationController的知识,以帮助您更全面地了解这个主题。

本文目录一览:

如何向 UINavigationController 添加右键?(添加ui界面)

如何向 UINavigationController 添加右键?(添加ui界面)

我正在尝试将刷新按钮添加到导航控制器的顶部栏,但没有成功。

这是标题:

@interface PropertyViewController : UINavigationController {}

这是我尝试添加它的方式:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {        UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain                                          target:self action:@selector(refreshPropertyList:)];              self.navigationItem.rightBarButtonItem = anotherButton;    }    return self;}

答案1

小编典典

尝试在 viewDidLoad 中执行此操作。一般来说,你应该尽可能推迟任何事情,直到那个时候,当一个 UIViewController
被初始化时,它可能仍然需要很长一段时间才能显示出来,尽早做工作和占用内存是没有意义的。

- (void)viewDidLoad {  [super viewDidLoad];  UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];            self.navigationItem.rightBarButtonItem = anotherButton;  // exclude the following in ARC projects...  [anotherButton release];}

至于为什么它目前不起作用,我不能在没有看到更多代码的情况下 100% 肯定地说,但是在 init 和视图加载之间发生了很多事情,你可能正在做一些导致
navigationItem 重置的事情之间。

DestinationViewController Segue和UINavigationController迅速

DestinationViewController Segue和UINavigationController迅速

所以我有这样的prepareForSegue方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {    if segue.identifier == "fromEventTableToAddEvent" {        let addEventViewController: AddEventViewController = segue.destinationViewController as! AddEventViewController        addEventViewController.newTagArray = newTagArray    }}

但是,在运行时,应用程序崩溃并记录了以下消息:

无法转换类型为’UINavigationController’的值

出现错误是因为addEventController嵌入在导航控制器中,但是我不确定如何设置segue,以便将destinationViewController设置为NavigationController,但还允许我将prepareForSegue方法中的变量传递给addEventController。

那么我将如何使用Swift做到这一点?

非常感谢您的帮助和时间。它可以帮助很多

答案1

小编典典

您只需要访问导航控制器的topViewController,它将是AddEventViewController。这是代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {    if segue.identifier == "fromEventTableToAddEvent" {        let nav = segue.destinationViewController as! UINavigationController        let addEventViewController = nav.topViewController as! AddEventViewController        addEventViewController.newTagArray = newTagArray    }}

Implementing Navigation with UINavigationControlle

问题:

     你想要允许你的用户在视图之间来回切换

解决方法:

     

     使用UINavigationController

讨论:

     选择Empty Application取什么名字你随意,然后选择菜单文件选择New->New File...选择第一个objective-c Class这个文件,命名为FirstViewController,继承自UINavigationController。

     基本文件搞定,然后我们开始,找到APP的代理文件 AppDelegate.m文件,然后写下如下代码:

                 #import “AppDelegate.h”

                 #import “FirstViewController.h”

                 @interface AppDelegate ()

                  @property(nonatiomic,strong) UINavigationController *navigationController;

                 @end

               @implementation AppDelegate

             …

    现在我们要使用 initWithRootViewController : 方法初始化导航栏,并且传递我们的参数。然后导航栏就作为根视图出现在窗口上。这里千万不要混淆,UINavigationController是UIViewController的子类,这里的RootViewController可以是任何对象,所以我们希望我们的根视图控制器是一个导航控制器,就只需设置导航控制器为根视图控制器。     那么开始动手写代码

      - (BOOL)  application:(UIApplication *)application didFinishLauchingWithOptions: (NSDictionary *) lauchOptions

  

{

   FirstViewController *viewController = [[FirstViewController alloc ] initWithName:nil bundle:nil];

   self.navigationController = [[UINavigationController alloc ] initWithRootViewController:viewController];

   self.window = [[UIWindow alloc] initWithName: [UIScreen mainScreen] bounds];

   

   self.window.rootViewController = self.navigationController;

   self.window.backgroundColor = [UIColor whiteColor];

  [self.window makeKeyAndVisible];

  

   return YES;

}

  现在在模拟器中运行这个应用看: 应该出现这个图片:

图:1-33 显示导航栏控制器

可能会注意到导航栏显示“First Controller”,这难道是新的部件?并不是,这也是导航条,我们会在很多的导航中使用到那个bar,这里的bar显示的是一个文本,每个视图控制器指定了一个标题本身,一旦视图控制器被压入堆栈,导航控制器就会自动显示这个文本!

  找到我们的根视图控制器的实现稳健,在ViewDidLoad方法里面,设置标题并且添加按钮,当用用户按下这个按钮,就会跳到第二个视图控制器! (还是在AppDelegate.m文件里,然后代码如下:)

      #import “FirstController.h”

      #import “SecondController.h”

     @interface FirstViewController ()

            @property (nonatomic, strong) UIButton *displaySecondViewController;

     @end

    

    @implemetation FirstViewController

 

    - (void) performDisplaySecondViewController: (id)parmamSender

{

     SecondViewController *secondController  = [[SecondViewController alloc] initWithName:nil  bundle:YES];

      [self.navigationContoller pushViewContoller : secondController  animated: YES];

}

   - (void)viewDidLoad

{

   [super viewDidLoad];

   self.title = @“First Controller”;

   

   self.displaySecondViewController  = [UIButton buttonWithType:UIButtonTypeSystem];

  [self.displaySecondViewController setTitle:@“Display Second View Controller”  forState:UIControlStateNormal];

  [self.displaySecondViewController sizeToFit];

  self.displaySecondViewController.center  = self.view.center;

 [self.displaySecondViewController addTarget:self  action:@selector (performDIsplaySecondViewController:) forControlEvents: UIControlEventTouchUpInside];

[self.view.addSubBiew: self.displaySecondViewController];

}

@end

现在创建第二个视图控制器,命名为SecondViewController,(就像创建FIrstViewController一样创建SecondViewController)既然创建了,那我们就给他一个标题

#import  “SecondViewController.h”

@implementation  SecondViewController

- (void)viewDidLoad

{

  [super viewDidLoad];

  self.title = @“Second Controller”;

}

两个视图都创建好了,接下来要做的就是实现从第一个视图跳转到第二个试图。使用popViewControllerAnimate:方法,取布尔作为参数。如果布尔值设置为YES ,就会在转到下一个视图控制器的时候会有动画效果,如果NO则反之。当显示屏幕上的第二个视图控制器,你会看到类似于图1-34所示的东西。

图:1-34

#import “SecondViewController


@implementation SecondViewController

- (void)viewDidLoad

{

   [super viewDidLoad];


   self.title = @“Second Controller”;

}


- (void)goBack

{

   [self.navigationController popViewControllerAnimates:YES];

}


- (void) viewDidAppear:(BOOL)paramAnimated

{

   [super viewDidAppear : paramAnimated];

   [self performSelector: @selector(goBack)  withObject:nil      afterDelay:5.0f];

}

@end


通过以上代码我们就完成了我们的需求了!



ios – UINavigationBar -pushNavigationItem在将新控制器推送到UINavigationController堆栈时从不调用

ios – UINavigationBar -pushNavigationItem在将新控制器推送到UINavigationController堆栈时从不调用

我今晚正在做一些测试来查看原生UINavigationBar的行为.我创建了一个简单的代码片段,它执行以下操作:
- (void)pushController {
    PHViewController *ctrl2 = [[[PHViewController alloc] initWithNibName:@"PHViewController" bundle:nil] autorelease];
    ctrl2.shouldShowPrompt = YES;
    [self.viewController pushViewController:ctrl2 animated:YES];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    PHViewController *ctrl = [[[PHViewController alloc] initWithNibName:@"PHViewController" bundle:nil] autorelease];
    ctrl.shouldShowPrompt = YES;
    ctrl.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Push" 
                                                                           style:UIBarButtonItemStyleDone
                                                                          target:self
                                                                          action:@selector(pushController)] autorelease];

    self.viewController = [[[PHNavigationController alloc] initWithRootViewController:ctrl] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

现在我已经将UINavigationController的UINavigationBar子类化了(我知道这是非法的,这是一个教育问题)我已经覆盖了以下方法:

- (void)setItems:(NSArray *)items animated:(BOOL)animated {
    NSLog(@"Setting Navigation Item");
    [super setItems:items animated:animated];
}

- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated  {
    NSLog(@"Pushing Navigation Item");
    [super pushNavigationItem:item animated:animated];
}

- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated {
    NSLog(@"Poping Navigation Item");
    return [super popNavigationItemAnimated:animated];
}

- (void)setValue:(id)value forKeyPath:(Nsstring *)keyPath {
    NSLog(@"Setting Value: %@ for keyPath:%@",value,keyPath);
    [super setValue:value forKeyPath:keyPath];
}

这是我的问题:为什么控制台中存在“弹出导航项”(因此被调用的方法)和“推送导航项”不是?

解决方法

我找到了原因:它调用 – (void)pushNavigationItem:(UINavigation *)项不调用 – (void)pushNavigationItem:animated!

还是非常感谢!

ios – UINavigationBar没有UINavigationController

ios – UINavigationBar没有UINavigationController

我想在我的应用程序的顶部有一个UINavigationBar,就像在手机应用程序中,但我不想使用它与UINavigationController,也就是我想使用它作为一个“独立”对象.

具有UINavigationBar的视图控制器具有.xib文件中的视图.我试图从对象库拖动一个UINavigationBar的实例,它的工作正常,但是应用程序的状态栏仍然是白色的,而UINavigationBar是灰色的.我想要状态栏具有相同的灰色调,但不是其余的视图.

告诉你我的意思,我有两张照片.第一个是手机应用程序.请注意,状态栏和导航栏为灰色,但背景为白色.

以下图片来自我的应用程序,因为您可以看到状态栏是白色的(我也希望它是灰色的).

我也试图在视图控制器中使用以下代码.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"Köp ProViva";
}

我尝试过它有无UINavigationBar,但是没有导航栏显示,或者看起来和以前一样.

如何添加一个UINavigationBar并且状态栏具有相同的颜色?

解决方法

您可以实现UINavigationBarDelegate协议的委托方法-positionForBar:简单地返回UIBarPositionTopAttached.

例:

-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
    return UIBarPositionTopAttached;
}
//if you're not using a `UINavigationController` and instead
//simply want a `UINavigationBar` then use the following method as well
-(void)testMethod
{
    UINavigationBar *navBar = [[UINavigationBar alloc] init];
    [navBar setFrame:CGRectMake(0,20,self.view.frame.size.width,44)];
    [navBar setBarTintColor:[UIColor lightGrayColor]];
    [navBar setDelegate:self];
    [self.view addSubview:navBar];
}

希望这可以帮助.

今天关于如何向 UINavigationController 添加右键?添加ui界面的分享就到这里,希望大家有所收获,若想了解更多关于DestinationViewController Segue和UINavigationController迅速、Implementing Navigation with UINavigationControlle、ios – UINavigationBar -pushNavigationItem在将新控制器推送到UINavigationController堆栈时从不调用、ios – UINavigationBar没有UINavigationController等相关知识,可以在本站进行查询。

本文标签: