【问题标题】:UIButton tapped to swap to another xib轻按 UIButton 以切换到另一个 xib
【发布时间】:2010-04-16 15:16:18
【问题描述】:

我的 MainWindow.xib 中有一个 UIButton

当我点击按钮时,我想交换视图。我该怎么做?

我还想在视图之间传输一些数据(例如颜色偏好和字符串)

任何可以找到答案的示例代码或链接都会非常有帮助。

【问题讨论】:

    标签: objective-c cocoa-touch iphone-sdk-3.0 uiview uibutton


    【解决方案1】:

    alloc 一个临时视图控制器,并调用initWithNibName:。然后拨打[self presentModalViewController:(the view controller you just made) animated:YES];(或否)。要传递数据,请在您的另一个视图控制器上创建一个方法,将其添加到其 .h 文件中,然后在您的第一个视图控制器的 .m 文件中,将其导入并使其成为一个类,然后调用 [theviewcontrollermadeearlier yourmethod:argument :argument etc.]; 例如:

    MyFirstViewController.h:

    #import <UIKit/UIKit.h>
    #import "MySecondViewController.h"
    ...
    @class MySecondViewController
    ...
    

    MyFirstViewController.m:

    ...
    MySecondViewController *tempVC = [[MySecondViewController alloc] initWithNibName:@"MySecondView"];
    [self presentModalViewController:tempVC animated:YES];
    [tempVC passDataWithString:@"a string" andColor:yellowcolor];
    

    MySecondViewController.h:

    @interface MySecondViewController : UIViewController {
    ...
    }
    - (void)passDataWithString:(NSString *)passedString andColor:(UIColor *)passedColor;
    

    MySecondViewController.m:

    ...
    - (void)passDataWithString:(NSString *)passedString andColor:(UIColor *)passedColor {
    // Do something
    }
    

    编辑: 为了让按钮触发这个,在你的第一个视图控制器的头文件中,在@interface部分添加IBOutlet IBAction *buttonPressed;,然后在}@end之间添加- (IBAction)buttonPressed;
    进入 Interface Builder,然后将 IBAction 连接到按钮。
    然后,在您的第一个视图控制器的主文件中,添加以下内容:

    - (IBAction)buttonPressed {
        // The code to execute when pressed
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-30
      • 2017-03-20
      • 2011-12-19
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多