UIButton轻拍交换到另一个xib

时间:2010-04-16 15:16:18

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

我的MainWindow.xib中有一个UIButton

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

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

任何示例代码或链接到我能找到答案的地方都非常有帮助。

1 个答案:

答案 0 :(得分: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
}

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

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