实施授权

时间:2012-03-08 13:31:14

标签: iphone objective-c ios delegates

我想我正在关注代表团的工作方式,here's the tutorial I followed,但我在某处弄乱了。我期待我的代表参加NSLog,但事实并非如此。任何人都可以找出我遗失或做错的事情吗?

我的 MainViewController.h

@interface MainViewController : UITableViewController < AddClassDelegate >

MainViewController.m

- (void)cancelAddingClass {
    NSLog(@"Canceled Yo");
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

/*
 When a row is selected, the segue creates the detail view controller as the destination.
 Set the detail view controller's detail item to the item associated with the selected row.
 */
if ([[segue identifier] isEqualToString:@"addClassSegue"]) {

    UINavigationController *nc = (UINavigationController *)segue.destinationViewController;
    AddClassViewController *addClassVC = (AddClassViewController *)[nc.viewControllers objectAtIndex:0];
    addClassVC.delegate = self;
}

我的模态视图控制器 AddClassViewController.h

@protocol AddClassDelegate <NSObject>
- (void)cancelAddingClass;
@end

@interface AddClassViewController : UITableViewController

@property (weak, nonatomic) id< AddClassDelegate > delegate;

- (IBAction)cancelButtonPressed:(id)sender;

AddClassViewController.m

@synthesize delegate;

- (IBAction)cancelButtonPressed:(id)sender {
    [self.delegate cancelAddingClass];
}

cancelButtonPressed: 

连接到故事板中的模态视图的“取消”按钮。

3 个答案:

答案 0 :(得分:2)

您的代码看起来很好,这表明问题是我们看不到的地方。我的猜测在这里:

AddClassViewController *addClassVC = [segue destinationViewController];
addClassVC.delegate = self;
NSLog(@"segued");

您是否在导航控制器中嵌入了模态视图控制器?如果是这样,destinationViewController将为您提供导航控制器,而不是AddClassViewController。检查调试器中实际上是哪个类addClassVC。

如果它是导航控制器,没问题,您只需要使用.viewControllers属性访问您的实际VC。在几行上,使其更容易理解:

UINavigationController *nc = (UINavigationController *)segue.destinationViewController;
AddClassViewController *addClassVC = (AddClassViewController *)[nc.viewControllers objectAtIndex:0];
addClassVC.delegate = self;

你可以用更少的行来完成它,但它是一堆乱码和嵌套括号,这很难调试。

答案 1 :(得分:0)

您的主视图控制器是否可能被释放?这会将弱引用设置为nil,并且您发送给您的代理的消息将被忽略,因为您将发送消息nil

答案 2 :(得分:-1)

一切都很完美。我没有看到任何问题。保持断点到处调试。

相关问题