我一直在研究代表。爲什麼不委派工作?
所以我寫代碼,然後運行此代碼。
你能告訴我有什麼問題嗎?
這是我的代碼。
ViewController2.h
@protocol ViewController2Delegate <NSObject>;
@required
-(void)practiceDelegateMethod:(Float32)var1 andVar2:(Float32)var2;
@end
@interface ViewController2 : UIViewController
@property (assign, nonatomic) id <CompressSetupViewControllerDelegate> delegate;
ViewController2.m
@synthesize delegate;
- (IBAction)compressSetupCancleAction:(id)sender {
[self.delegate practiceDelegateMethod:var1 andVar2:var2];
[self dismissViewControllerAnimated:YES completion:nil];
}
ViewController1.h
@interface HomeViewController : UIViewController<ViewController2Delegate>
ViewController1.m
-(void) practiceDelegateMethod:(Float32)var1 andVar2:(Float32)var2{
NSLog(@"delegate var1 : %@ var2 : %@",[NSString stringWithFormat:@"%f",var1],[NSString stringWithFormat:@"%f",var2]);
}
上ViewController1,你有沒有設置ViewController2。委託=自我; ? –
@CongTran是的,設置viewDidLoad。 –
@CongTran ViewController2Delegate * vc2 = [[ViewController2Delegate alloc] init]; vc2.delegate = self; –