Reputation: 644
Hi i created UInavigation controller in iOS programatically but the problem is when i move to next view controller i am directing to same screen.I created another view with name InfoView but when i push to InfoView it shows the same screen with buttons that i created in 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];
}
]1
Upvotes: 0
Views: 39
Reputation: 6903
You can't push a View. You need to push a View Controller.
If you want to show the View on top of the same View Controller, then you need to add the InfoView as the subView, preferably with animation.
But if you wanted to push the InfoView then you should create a UIViewController in your Storyboard and then add the InfoView as that View Controller's subview.
Let's say you create a new View Controller called InfoViewcontroller
which is going to hold the InfoView. Now go to the InfoViewcontroller's -viewDidLoad
method and add the following line
InfoView *infoVC=[[InfoView alloc]initWithFrame:self.view.frame];
[self.view addSubview:infoVC];
However, as you don't want to touch your storyboard, you may want to add it as a subView with animation. So, you can change your Look
method with something like-
-(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];
}
When the InfoView.m looks like-
#import "InfoView.h"
@implementation InfoView
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if(self){
self.backgroundColor=[UIColor whiteColor];
}
return self;
}
@end
Note: you need an -initWithFrame
method as your InfoView needs to know it's frame size.
Upvotes: 1
Reputation: 38162
A navigation controller works like a stack so you can push and pop viewcontroller exactly like a stack behaves in the real world. Think of a navigation controller as a stack of viewcontrollers, so you cant actually add a UIView
, but what you would like to do instead is to add the uiview as a subview in your view controller and push that view controller onto the navigation controller stack with a simple:
[navigationcontroller pushViewController:aViewcontroller];
Please read through Apple Documentation for more insight.
Upvotes: 1