在Objective C中切换视图

时间:2016-10-31 00:16:12

标签: ios 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.

我正在创建一个新的视图dealerview并添加和删除子视图视图。我错了,但仍然无法理解。    看起来我在 - (BOOL)应用程序方法中出错了。

2 个答案:

答案 0 :(得分:1)

在app委托中执行此类操作非常不典型。最好在UIViewController的子类中执行此操作,并将该子类的实例添加为rootViewController的{​​{1}} UINavigationController,使其成为didFinishLaunchingWithOptions中的密钥并显示。

您可能也不希望从其超级视图中删除UIViewController的根视图。目前,您需要为从视图层次结构中删除其超级视图vc.view的按钮创建操作。问题是view1self.mainView是相同的,所以:

UIView *view1=[sender superview];
[view1 removeFromSuperview];
[self.mainView addSubview:view];

您要从视图层次结构中删除视图,然后向视图中添加一个视图,该视图已被删除并希望能够看到它。

您应该有一个视图充当button1的容器,另一个视图充当button2的容器,但是都添加到self.mainView或从<div>Some tags are <b> and <div></div> 删除。

答案 1 :(得分:0)

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

我确实使用setHidden隐藏了视图:YES以便在视图之间切换回来。

相关问题