实现模式视图控制器数据传输的委托方法 [英] Implementing delegate methods for modal view controller data transfer

查看:133
本文介绍了实现模式视图控制器数据传输的委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的项目来呈现一个模态视图控制器,并根据按下的模态VC中的哪个按钮传回一个字符串。我基于这一切都是在iTunes U上看斯坦福课程。看起来我有一切正确,但是我得到了一些编译器警告。



首先我得到一个 TransferViewController.m


中的不兼容指针类型
传递'setDelegate:

第二个我收到四个警告称为无效的接收者类型'id< MyModalViewControllerDelegate> *'但这些不会显示在构建结果区域在 MyModalViewController.m 中的违规行旁边,每个按钮操作中的两行。



这是代码...

  // TransferViewController.h 

#import< UIKit / UIKit.h>
#importMyModalViewController.h;

@interface TransferViewController:UIViewController< MyModalViewControllerDelegate> {
UILabel * label;
UIButton *按钮;
}

@property(nonatomic,retain)IBOutlet UILabel * label;
@property(nonatomic,retain)UIButton *按钮;

- (IBAction)updateText;

@end






  // TransferViewController.m 

#importTransferViewController.h

@implementation TransferViewController

@synthesize标签;
@synthesize按钮;

- (IBAction)updateText {
MyModalViewController * myModalViewController = [[MyModalViewController alloc] init];
myModalViewController.delegate = self; //我在这里得到警告。
[self presentModalViewController:myModalViewController animated:YES];
[myModalViewController release];
}

- (void)myModalViewController :( MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog {
label.text = selectedDog;
[self dismissModalViewControllerAnimated:YES];
}

@end






  // MyModalViewController.h 

#import< UIKit / UIKit.h>

@protocol MyModalViewControllerDelegate;

@interface MyModalViewController:UIViewController {
UIButton * abby;
UIButton * zion;
id< MyModalViewControllerDelegate>代表;
}

@property(assign)id< MyModalViewControllerDelegate>代表;

- (IBAction)selectedAbby;
- (IBAction)selectedZion;

@end

@protocol MyModalViewControllerDelegate< NSObject>

@optional

- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog;

@end






  // MyModalViewController.m 

#importMyModalViewController.h

@implementation MyModalViewController

@synthesize代表;

- (IBAction)selectedAbby {
if([self.delegate responsesToSelector:@selector(myModalViewController:didFinishSelecting :)]){
[self.delegate myModalViewController:self didFinishSelecting: @Abby];
}
}

- (IBAction)selectedZion {
if([self.delegate responsesToSelector:@selector(myModalViewController:didFinishSelecting :)]){
[self.delegate myModalViewController:self didFinishSelecting:@Zion];
}

}


解决方案

id< something> 之前和委托之前,删除 *



所以使这个

  id< MyModalViewControllerDelegate> *代表; 

这个

  id< MyModalViewControllerDelegate>代表; 


I have a simple project to present a modal view controller and transfer back a string based on which button in the modal VC that gets pressed. I based it all on watching the Stanford class on iTunes U. It looks like I have everything correct, but I get a couple of compiler warnings.

First I get one called passing argument 1 of 'setDelegate:' from incompatible pointer type in TransferViewController.m

Second I get four warnings called Invalid receiver type 'id <MyModalViewControllerDelegate>*' but these aren't displayed in the build results area, rather next to the offending lines in MyModalViewController.m, both lines in each of the button actions.

Here's the code...

//  TransferViewController.h

#import <UIKit/UIKit.h>
#import "MyModalViewController.h";

@interface TransferViewController : UIViewController <MyModalViewControllerDelegate> {
    UILabel *label;
    UIButton *button;
}

@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) UIButton *button;

- (IBAction)updateText;

@end


//  TransferViewController.m

#import "TransferViewController.h"

@implementation TransferViewController

@synthesize label;
@synthesize button;

- (IBAction)updateText {
    MyModalViewController *myModalViewController = [[MyModalViewController alloc] init];
    myModalViewController.delegate = self; // I get the warning here.
    [self presentModalViewController:myModalViewController animated:YES];
    [myModalViewController release];
}

- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog {
    label.text = selectedDog;
    [self dismissModalViewControllerAnimated:YES];
}

@end


//  MyModalViewController.h

#import <UIKit/UIKit.h>

@protocol MyModalViewControllerDelegate;

@interface MyModalViewController : UIViewController {
    UIButton *abby;
    UIButton *zion;
    id <MyModalViewControllerDelegate> delegate;
}

@property (assign) id <MyModalViewControllerDelegate> delegate;

- (IBAction)selectedAbby;
- (IBAction)selectedZion;

@end

@protocol MyModalViewControllerDelegate <NSObject>

@optional

- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog;

@end


//  MyModalViewController.m

#import "MyModalViewController.h"

@implementation MyModalViewController

@synthesize delegate;

- (IBAction)selectedAbby {
    if ([self.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) {
        [self.delegate myModalViewController:self didFinishSelecting:@"Abby"];
    }
}

- (IBAction)selectedZion {
    if ([self.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) {
        [self.delegate myModalViewController:self didFinishSelecting:@"Zion"];
    }

}

解决方案

Get rid of those *s after id <something> and before delegate.

So make this

id <MyModalViewControllerDelegate> *delegate;

this

id <MyModalViewControllerDelegate> delegate;

这篇关于实现模式视图控制器数据传输的委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆