在視圖中調用,讓我們把它的firstView我創建了一個secondView如下推它,如果某些事情發生在的firstView:委託方法沒有得到來自第二視圖
SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
現在當我在第二個視圖中,如果讓我說一個按鈕被按下,我想回到firstView,並從secondView傳回第一個視圖的值(比如說從secondView到第一個視圖的textfield的整數值)。
這裏是我的嘗試:
@protocol SecondViewControllerDelegate;
#import <UIKit/UIKit.h>
#import "firstViewController.h"
@interface SecondViewController : UIViewController <UITextFieldDelegate>
{
UITextField *xInput;
id <SecondViewControllerDelegate> delegate;
}
- (IBAction)useXPressed:(UIButton *)sender;
@property (assign) id <SecondViewControllerDelegate> delegate;
@property (retain) IBOutlet UITextField *xInput;
@end
@protocol SecondViewControllerDelegate
- (void)secondViewController:(SecondViewController *)sender xValue:(int)value;
@end
而在M檔
- (IBAction)useXPressed:(UIButton *)sender
{
[self.delegate secondViewController:self xValue:1234]; // 1234 is just for test
}
然後在的firstView我所做的:
#import "SecondViewController.h"
@interface FirstViewController : UITableViewController <SecondViewControllerDelegate> {
}
@end
並實現了:
- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
[self.navigationController popViewControllerAnimated:YES];
}
現在,問題是在FirstViewController中的一個我得到的警告「找不到協議」SecondViewControllerDelegate「的定義,並且對於兩個委託方法(上面的最後一段代碼)根本不會被調用。有人可以告訴我什麼是錯的嗎?
是你確定委託!= nil? – Mehdzor
我應該做什麼以及在哪裏確保代表不是零? – Dogahe
您是否設置了代表?像secondVC.delegate = self; – Mehdzor