您應該使用代表團做THI小號
在ViewController1的.H的@implementation
@protocol ViewController1Delegate;
和上述添加此那麼這將下面@end
@protocol ViewController1Delegate <NSObject>
- (void)viewController1:(id)controller didDoAction;
@end
然後在控制器聲明類型ViewController1Delegate的屬性
@property (nonatomic, assign) id <ViewController1Delegate> delegate;
然後將其添加到ViewController1的.m中
@synthesize delegate;
,並在行動時要調用的委託方法
if ([self.delegate respondsToSelector:@selector(viewController1:didDoAction)]) {
[self.delegate performSelector:@selector(viewController1:didDoAction) withObject:self];
}
現在在ViewController2 .H impot ViewController1.h
#import ViewController1.h
現在當上ViewController2 .H監聽代表像此
@interface ViewController2 : UIViewController <ViewController1Delegate>
,然後在.M實現該方法
- (void)viewController1:controller didDoAction
{
//implement
}
當視圖控制器被實例化一個需要爲viewController1委託設定爲viewController2這樣
viewController1 = [ViewController1 new];
viewController2 = [ViewController2 new];
viewController1.delegate = viewController2
來源
2012-03-27 07:24:10
mOp
可以使用委託相同。爲視圖控制器創建自定義委託並處理操作。願它幫助你。 – Mangesh 2012-03-27 06:06:22