如何在没有导航控制器的情况下改变iPhone中按钮点击的视图?

时间:2021-08-02 20:35:03

I want to know how to change view on button click in iPhone without navigation controller?

我想知道如何在没有导航控制器的情况下改变按钮点击iPhone的视图?

5 个解决方案

#1


4  

If you're doing this with a UIViewController, you would probably do this like following:

如果你用UIViewController做这个,你可能会这样做:

- (IBAction)change {
    UIViewController* viewController = [[UIViewController alloc] init];
    [self.view addSubView];
    // Save the UIViewController somewhere if necessary, otherwise release it
}

Not sure why you don't want to use a UINavigationController though. If it's the navigation bar at the top you don't want to see, there's a property for that so you can hide that bar. Not sure about it's name, probably navigationBarHidden or something like that. You can look that up in the API.

不知道为什么不需要使用UINavigationController。如果你不想看到顶部的导航栏,这里有一个属性,你可以隐藏它。不知道它的名字,可能是navigationBarHidden之类的。可以在API中查找。

#2


3  

There are many different ways you can do that, and you should possibly provide more information about your app.

有很多不同的方法可以做到这一点,你应该提供更多关于你的应用的信息。

A generic way to do it is the following, with animation:

一种通用的方法是使用动画:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

[vc1 viewWillDisappear:YES];
[vc2 viewWillAppear:YES];
vc1.view.hidden = YES;
vc2.view.hidden = NO;
[vc1 viewDidDisappear:YES];
[vc2 viewDidAppear:YES];

[UIView commitAnimations];

In this case, both views are there, you only hide/show them as you need. Alternatively, you could add/remove the view from their superview:

在这种情况下,两个视图都在那里,您只需要隐藏/显示它们就可以了。或者,你也可以从它们的父视图中添加/删除视图:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

[vc1 viewWillDisappear:YES];
[vc2 viewWillAppear:YES];
[vc1 removeFromSuperview];
[masterController.view addSubview:vc2.view;
[vc1 viewDidDisappear:YES];
[vc2 viewDidAppear:YES];

[UIView commitAnimations];

#3


1  

Place this in Button IBAction method

将其放在按钮IBAction方法中

[mCurrentView removeFromSuperview];
[self.view addSubView:mNewView];

[mCurrentView removeFromSuperview];(自我。视图addSubView mNewView):;

#4


1  

Assume you have two view myView1 and myView2 and one button myUIButton in memory

假设在内存中有两个视图myView1和myView2和一个按钮myUIButton

myView1.tag = 1;
myView2.tag = 2;
myUIButton.tag = 1; //Current visible view is myView1

-(void) ButtonCLicked:(id) sender 
{
       UIButton* myButton = (UIButton*)sender;


       if(sender.tag == 1)
       {
           [myView1 removeFromSuperview];
           [self addSubview:myView2]
           myButton.tag = myView2.tag;
       }
       else 
      {
          [myView2 removeFromSuperview];
          [self addSubview:myView1]
           myButton.tag = myView1.tag;
      }
}

#5


0  

You can place two UIView variables in the header file and hide/show one of them or you can make another layer over the existing view like this:

你可以在头文件中放置两个UIView变量并隐藏/显示其中一个或者你可以在现有的视图上创建另一层如下所示:

-(IBAction)changeView {

    UIView *view = [[UIView alloc] initWithNibName:@"letMeSeeThis" bundle:nil];
    [self.view addSubview:view];
}

#1


4  

If you're doing this with a UIViewController, you would probably do this like following:

如果你用UIViewController做这个,你可能会这样做:

- (IBAction)change {
    UIViewController* viewController = [[UIViewController alloc] init];
    [self.view addSubView];
    // Save the UIViewController somewhere if necessary, otherwise release it
}

Not sure why you don't want to use a UINavigationController though. If it's the navigation bar at the top you don't want to see, there's a property for that so you can hide that bar. Not sure about it's name, probably navigationBarHidden or something like that. You can look that up in the API.

不知道为什么不需要使用UINavigationController。如果你不想看到顶部的导航栏,这里有一个属性,你可以隐藏它。不知道它的名字,可能是navigationBarHidden之类的。可以在API中查找。

#2


3  

There are many different ways you can do that, and you should possibly provide more information about your app.

有很多不同的方法可以做到这一点,你应该提供更多关于你的应用的信息。

A generic way to do it is the following, with animation:

一种通用的方法是使用动画:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

[vc1 viewWillDisappear:YES];
[vc2 viewWillAppear:YES];
vc1.view.hidden = YES;
vc2.view.hidden = NO;
[vc1 viewDidDisappear:YES];
[vc2 viewDidAppear:YES];

[UIView commitAnimations];

In this case, both views are there, you only hide/show them as you need. Alternatively, you could add/remove the view from their superview:

在这种情况下,两个视图都在那里,您只需要隐藏/显示它们就可以了。或者,你也可以从它们的父视图中添加/删除视图:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

[vc1 viewWillDisappear:YES];
[vc2 viewWillAppear:YES];
[vc1 removeFromSuperview];
[masterController.view addSubview:vc2.view;
[vc1 viewDidDisappear:YES];
[vc2 viewDidAppear:YES];

[UIView commitAnimations];

#3


1  

Place this in Button IBAction method

将其放在按钮IBAction方法中

[mCurrentView removeFromSuperview];
[self.view addSubView:mNewView];

[mCurrentView removeFromSuperview];(自我。视图addSubView mNewView):;

#4


1  

Assume you have two view myView1 and myView2 and one button myUIButton in memory

假设在内存中有两个视图myView1和myView2和一个按钮myUIButton

myView1.tag = 1;
myView2.tag = 2;
myUIButton.tag = 1; //Current visible view is myView1

-(void) ButtonCLicked:(id) sender 
{
       UIButton* myButton = (UIButton*)sender;


       if(sender.tag == 1)
       {
           [myView1 removeFromSuperview];
           [self addSubview:myView2]
           myButton.tag = myView2.tag;
       }
       else 
      {
          [myView2 removeFromSuperview];
          [self addSubview:myView1]
           myButton.tag = myView1.tag;
      }
}

#5


0  

You can place two UIView variables in the header file and hide/show one of them or you can make another layer over the existing view like this:

你可以在头文件中放置两个UIView变量并隐藏/显示其中一个或者你可以在现有的视图上创建另一层如下所示:

-(IBAction)changeView {

    UIView *view = [[UIView alloc] initWithNibName:@"letMeSeeThis" bundle:nil];
    [self.view addSubview:view];
}