Ios 在Objective C中切换视图

Ios 在Objective C中切换视图,ios,objective-c,Ios,Objective C,我正在创建一个新的视图DealView,并在其中添加和删除子视图视图。我出了点问题,但还是没办法解决。 看起来我在-(BOOL)应用程序方法中出错了。在应用程序委托中执行此类操作是非常不典型的。最好在UIViewController的子类中执行此操作,并将该子类的一个实例添加为UINavigationController的rootViewController,您可以在didfishlaunchwithoptions中设置键并使其可见 您也可能不希望从超级视图中删除UIViewController

我正在创建一个新的视图DealView,并在其中添加和删除子视图视图。我出了点问题,但还是没办法解决。
看起来我在-(BOOL)应用程序方法中出错了。

在应用程序委托中执行此类操作是非常不典型的。最好在
UIViewController
的子类中执行此操作,并将该子类的一个实例添加为
UINavigationController
rootViewController
,您可以在
didfishlaunchwithoptions
中设置键并使其可见

您也可能不希望从超级视图中删除
UIViewController
的根视图。目前,您关心的是为按钮创建一个操作,以从视图层次结构中删除其superview,
vc.view
。问题是
view1
self.mainView
与以下视图相同:

- (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.
您正在从视图层次结构中删除一个视图,然后将一个视图添加到刚刚删除并希望看到它的视图中

您应该有一个视图作为
button1
的容器,另一个视图作为
button2
的容器,但都添加到或从
self.mainView
中删除

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

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

我的分配不应使用任何控制器。在本作业中,我应该只切换视图。@Harish如果您正在与系统的标准模式进行激烈的斗争,这很好地表明您做得不对,或者。。。至少以一种完全非标准的方式,这在将来很可能会被打破。@Harish I如果您持有对正在使用的视图的引用,可能会更容易操纵视图层次结构。为
dealerView
UIView*view1=[[UIView alloc]initWithFrame:self.mainView.bounds]创建属性。这样你就可以避免类似于
UIView*view2=[[sender superview]superview]这样的事情
将子视图添加到
dealView
中,但从未将其添加到视图层次结构中。例如
[self.mainView addSubview:dealerView]
UIButton *addVehicleBackbtn=button;
UIView *addVehicleView=[addVehicleBackbtn superview];
[addVehicleView setHidden:YES];