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

Implementing delegate methods for modal view controller data transfer(为模态视图控制器数据传输实现委托方法)
本文介绍了为模态视图控制器数据传输实现委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

首先我得到一个名为 passing argument 1 of 'setDelegate:' from incompatible pointer type in TransferViewController.m

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

其次,我收到四个警告,称为 Invalid receiver type 'id <MyModalViewControllerDelegate>*' 但这些未显示在构建结果区域中,而是显示在 MyModalViewController 中的违规行旁边.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.

这是代码...

//  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>之后delegate<之前的那些*/代码>.

做这个

id <MyModalViewControllerDelegate> *delegate;

这个

id <MyModalViewControllerDelegate> delegate;

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)