【问题标题】:How to change view on button click in iPhone without navigation controller?如何在没有导航控制器的情况下更改 iPhone 中按钮单击的视图?
【发布时间】:2011-09-11 13:07:07
【问题描述】:

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

【问题讨论】:

    标签: iphone objective-c views


    【解决方案1】:

    您可以通过多种不同的方式做到这一点,并且您可能应该提供有关您的应用的更多信息。

    使用动画的通用方法如下:

    [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];
    

    在这种情况下,两个视图都存在,您只需根据需要隐藏/显示它们。或者,您可以从他们的超级视图中添加/删除视图:

    [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];
    

    【讨论】:

      【解决方案2】:

      假设您在内存中有两个视图 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;
            }
      }
      

      【讨论】:

        【解决方案3】:

        如果您使用 UIViewController 执行此操作,您可能会执行以下操作:

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

        不知道为什么你不想使用 UINavigationController。如果它是您不想看到的顶部的导航栏,则有一个属性,因此您可以隐藏该栏。不确定它的名称,可能是 navigationBarHidden 或类似的东西。您可以在 API 中查找。

        【讨论】:

          【解决方案4】:

          把这个放在Button IBAction方法中

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

          【讨论】:

            【解决方案5】:

            您可以在头文件中放置两个 UIView 变量并隐藏/显示其中一个,或者您可以像这样在现有视图上再创建一层:

            -(IBAction)changeView {
            
                UIView *view = [[UIView alloc] initWithNibName:@"letMeSeeThis" bundle:nil];
                [self.view addSubview:view];
            }
            

            【讨论】:

            • UIView 没有属性 initWithNibName
            • 是的 UIview 没有 initWithNibName 而不是它有 initWithFrame 和 initWithCoder
            猜你喜欢
            • 2013-08-28
            • 1970-01-01
            • 2011-12-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-08-09
            • 1970-01-01
            相关资源
            最近更新 更多