Harish Harish - 19 days ago 6
iOS Question

Switching between views in Objective C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// UIScreen represents the entire screen. Its bounds method
// returns the rectangle that corresponds to the entire screen
// CGRect is a C struct that used to group four floating
// point values that represent the x and y coordinates of the upper left
// corner, as well as the width and the height.
//

CGRect windowRect = [[UIScreen mainScreen] nativeBounds];
UIWindow *window = [[UIWindow alloc] initWithFrame:windowRect];
[window setBackgroundColor:[UIColor lightGrayColor]];

[self setWindow:window];
UIViewController *vc = [[UIViewController alloc]init];
window.rootViewController = vc;

self.mainView = vc.view;
[window makeKeyAndVisible];

//Window is now visible
//Add created views to the mainView to make them visible.

//Example : add a "Hello World" label
UIView *dealerView=[[UIView alloc]initWithFrame:self.mainView.bounds];

UIView *view1=[[UIView alloc]initWithFrame:self.mainView.bounds];
CGRect labelRect = { 150.0, 150.0, 150.0, 50.0 };
UIButton *button1 = [[UIButton alloc] initWithFrame:labelRect];
[button1 setBackgroundColor:[UIColor blackColor]];
[button1 setTitle:@"ENTER HERE" forState: (UIControlState)UIControlStateNormal];
[button1 addTarget:self action:@selector(enterHereClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setUserInteractionEnabled:YES];

[view1 addSubview:button1];

[dealerView addSubview:view1];

return YES;
}

-(void) enterHereClicked:(UIButton *) button{

CGRect labelRect = { 150.0, 400.0, 150.0, 50.0 };

UIButton *sender=button;
UIView *view1=[sender superview];
UIView *view2=[[sender superview]superview];
[view1 removeFromSuperview];

UIView *view=[[UIView alloc]initWithFrame:self.mainView.bounds];

UIButton *button2 = [[UIButton alloc] initWithFrame:labelRect];
[button2 setBackgroundColor:[UIColor blackColor]];
[button2 setTitle:@"Another View" forState: (UIControlState)UIControlStateNormal];
[button2 addTarget:self action:@selector(clickView:) forControlEvents:
UIControlEventTouchUpInside];

[view addSubview:button2];
[view2 addSubview: view];

}
-(void)clickView:(UIButton *) button{

NSLog(@"anand");
UIButton *sender=button;
UIView *view1=[sender superview];
UIView *view3=[[sender superview]superview];
[view1 removeFromSuperview];
CGRect labelRect = { 150.0, 400.0, 150.0, 50.0 };
UIView *view=[[UIView alloc]initWithFrame:labelRect];
[view setBackgroundColor:[UIColor greenColor]];

[view3 addSubview:view];



}



I have edited the code as per you advise. But still not working.


I am creating a new view dealerview and adding and removing the subviews views from it. I am going somewhere wrong but still could not figure it out.
Looks like I am going wrong in the -(BOOL) application method.

Answer
UIButton *addVehicleBackbtn=button;
UIView *addVehicleView=[addVehicleBackbtn superview];
[addVehicleView setHidden:YES];

I did hide the View using setHidden:YES in order to switch back between the views.