Uinavigation Controller问题

时间:2015-10-30 07:51:57

标签: ios

嗨我以编程方式在iOS中创建了UInavigation控制器,但问题是当我移动到下一个视图控制器时,我指向同一个屏幕。我创建了另一个名为 InfoView 的视图,但当我推送到 InfoView 它显示了与我在ViewController中创建的按钮相同的屏幕。

#import "AppDelegate.h"
    #import "ViewController.h"

    @interface AppDelegate ()
    {
        UINavigationController *navigation;

    }
    @end

    @implementation AppDelegate


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        navigation = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
        self.window.rootViewController = navigation;

        [self.window makeKeyAndVisible];
        return YES;
    }


#import "ViewController.h"
#import "InfoView.h"

@interface ViewController ()
{
    UIButton *RealVideo,*listen,*Readmat,*Look,*DiveIn;

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor=[UIColor whiteColor];
    [self designDeclare];
}
-(void)designDeclare{

    RealVideo=[UIButton buttonWithType:UIButtonTypeCustom];
    RealVideo.frame=CGRectMake(10,self.view.frame.size.height*0.2, CGRectGetWidth(self.view.frame)-20, CGRectGetHeight(self.view.frame)/7);
    [RealVideo addTarget:self action:@selector(real:) forControlEvents:UIControlEventTouchUpInside];
    [RealVideo setTitle:@"Real Video" forState:UIControlStateNormal];
    RealVideo.backgroundColor=[UIColor colorWithRed:0.744f green:0.000f blue:0.000f alpha:1.00f];
    [self.view addSubview:RealVideo];

    listen=[UIButton buttonWithType:UIButtonTypeCustom];
    listen.frame=CGRectMake(CGRectGetMinX(RealVideo.frame),CGRectGetMaxY(RealVideo.frame)+20, CGRectGetWidth(RealVideo.frame), CGRectGetHeight(RealVideo.frame));
   // [listen addTarget:self action:@selector(Listen:) forControlEvents:UIControlEventTouchUpInside];
    [listen setTitle:@"Listen" forState:UIControlStateNormal];
    listen.backgroundColor=[UIColor purpleColor];
    [self.view addSubview:listen];

    Readmat=[UIButton buttonWithType:UIButtonTypeCustom];
    Readmat.frame=CGRectMake(CGRectGetMinX(RealVideo.frame),CGRectGetMaxY(listen.frame)+20, CGRectGetWidth(RealVideo.frame), CGRectGetHeight(RealVideo.frame));
   // [Readmat addTarget:self action:@selector(Listen:) forControlEvents:UIControlEventTouchUpInside];
    [Readmat setTitle:@"Reading Material" forState:UIControlStateNormal];
    Readmat.backgroundColor=[UIColor colorWithRed:0.000f green:0.412f blue:0.851f alpha:1.00f];
    [self.view addSubview:Readmat];

    Look=[UIButton buttonWithType:UIButtonTypeCustom];
    Look.frame=CGRectMake(CGRectGetMinX(RealVideo.frame),CGRectGetMaxY(Readmat.frame)+20, CGRectGetWidth(RealVideo.frame), CGRectGetHeight(RealVideo.frame));
    [Look addTarget:self action:@selector(Look) forControlEvents:UIControlEventTouchUpInside];
    [Look setTitle:@"Look" forState:UIControlStateNormal];
    Look.backgroundColor=[UIColor colorWithRed:0.400f green:1.000f blue:0.400f alpha:1.00f];
    [self.view addSubview:Look];

    DiveIn=[UIButton buttonWithType:UIButtonTypeCustom];
    DiveIn.frame=CGRectMake(self.view.frame.size.width*0.7,CGRectGetMaxY(Look.frame)+20, CGRectGetWidth(RealVideo.frame)/4,CGRectGetHeight(RealVideo.frame)/1.5);
    [DiveIn setTitle:@"Dive In" forState:UIControlStateNormal];
    DiveIn.backgroundColor=[UIColor colorWithRed:0.941f green:0.471f blue:0.353f alpha:1.00f];
    [self.view addSubview:DiveIn];

}

-(void)real:(id)sender{



}

-(void)Look{

    InfoView *infoVC=[[InfoView alloc]init];
    [self.navigationController pushViewController:infoVC animated:YES];
}

This is my firstView controller and Second viewcontroller **InfoView**[![][1]] 1

2 个答案:

答案 0 :(得分:1)

您无法推送视图。您需要推送视图控制器。

如果要在同一个View Controller的顶部显示View,则需要将InfoView添加为subView,最好是动画。

但是如果您想要 推送 InfoView,那么您应该在Storyboard中创建一个UIViewController,然后将InfoView添加为View Controller的子视图。

假设您创建了一个名为InfoViewcontroller的新视图控制器,它将保存InfoView。现在转到InfoViewcontroller的-viewDidLoad方法并添加以下行

 InfoView *infoVC=[[InfoView alloc]initWithFrame:self.view.frame];
 [self.view addSubview:infoVC];

但是,由于您不想触摸故事板,因此您可能希望将其添加为带动画的子视图。因此,您可以使用类似

的方式更改Look方法
-(void)Look{

    InfoView *infoVC=[[InfoView alloc]initWithFrame:self.view.frame];
    [UIView transitionWithView:self.view
                      duration:1.0
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        [self.view addSubview:infoVC];
                    }
                    completion:NULL];

}

当InfoView.m看起来像 -

#import "InfoView.h"

@implementation InfoView

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        self.backgroundColor=[UIColor whiteColor];

    }
    return self;
}
@end

注意:您需要一个-initWithFrame方法,因为您的InfoView需要知道它的帧大小。

答案 1 :(得分:1)

导航控制器的工作方式类似于堆栈,因此您可以像在现实世界中的堆栈行为一样推送和弹出viewcontroller。将导航控制器视为一组视图控制器,因此您实际上无法添加UIView,但您想要做的是将uiview作为子视图添加到视图控制器中并将该视图控制器推送到导航控制器堆栈简单:

[navigationcontroller pushViewController:aViewcontroller];

请仔细阅读Apple Documentation了解更多信息。