加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 为模态视图控制器数据传输实现委托方法

发布时间:2020-12-14 17:36:17 所属栏目:百科 来源:网络整理
导读:我有一个简单的项目来呈现一个模态视图控制器,并根据按下的模式VC中的哪个按钮传回一个字符串.我基于在iTunes U上观看Stanford课程.我看起来一切都正确,但我得到了一些编译器警告. 首先,我从TransferViewController.m中的不兼容指针类型中得到一个名为’setD
我有一个简单的项目来呈现一个模态视图控制器,并根据按下的模式VC中的哪个按钮传回一个字符串.我基于在iTunes U上观看Stanford课程.我看起来一切都正确,但我得到了一些编译器警告.

首先,我从TransferViewController.m中的不兼容指针类型中得到一个名为’setDelegate:’的传递参数1

其次,我得到四个警告称为无效接收器类型’id< MyModalViewControllerDelegate> *’但这些警告不显示在构建结果区域中,而是显示在MyModalViewController.m中的违规行旁边,每个按钮操作中都有两行.

这是代码……

//  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"];
    }

}

解决方法

在id< something>之后摆脱那些* s在代表之前.

所以说吧

id <MyModalViewControllerDelegate> *delegate;

这个

id <MyModalViewControllerDelegate> delegate;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读