如何在iPhone上使用委托模式 [英] How to use delegate pattern on iPhone

查看:87
本文介绍了如何在iPhone上使用委托模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些关于在iPhone上使用委托图案的问题。



这是使用委托patten的代码。这个代码可以工作。

  SecondViewController * secondViewController = [[SecondViewController alloc] init]; 
secondViewController.delegate = self;
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];

我写了这样的代码。但它不行。我认为代理无效。

  GoogleDocsViewController * googleDocsView = [[GoogleDocsViewController alloc] initWithNibName:@GoogleDocsViewControllerbundle:nil ]; 
googleDocsViewController.delegate = self;
[self.navigationController pushViewController:googleDocsView animated:YES];
[googleDocsView release];

我不知道为什么这段代码有警告。



类'RootViewController'不实现'GoogleDocsViewControllerDelegate'协议



我该怎么做?



添加来源代码

  GoogleViewController.h 

@protocol GoogleDocsViewControllerDelegate;
@interface GoogleDocsViewController:UIViewController< UITextFieldDelegate> {
id< GoogleDocsViewControllerDelegate>代表;
}
@property(nonatomic,assign)id< GoogleDocsViewControllerDelegate>代表;

@end

@protocol GoogleDocsViewControllerDelegate< NSObject> ;;

@required

- (void)googleViewControllerInputDidEnd :( NSString *)text;

@end

googleDocsViewController.m

   - (void)googleViewControllerInputDidEnd :( NSString *)text {
NSLog(@delegate);
}

这个googleViewControllerInputDidEnd :( NSString *)文本方法是测试源。 >

解决方案

你能告诉我们哪个类正在创建GoogleDocsViewController吗?检查该类的头文件(.h文件),看看它是否实现了GoogleDocsViewControllerDelegate。



基本上,委托是从GoogleDocsViewController收听有趣的事件的对象,应该实现所提到的协议,以便知道在触发事件时执行哪些操作。


I have some questions about using delegate patten on iPhone.

This is code using delegate patten. This code works.

SecondViewController *secondViewController = [[SecondViewController alloc] init];   
    secondViewController.delegate = self;
    [self.navigationController pushViewController:secondViewController animated:YES];
    [secondViewController release];

I wrote code like this. But it doesn't work. I thought that delegate is not valid.

GoogleDocsViewController *googleDocsView = [[GoogleDocsViewController alloc]initWithNibName:@"GoogleDocsViewController" bundle:nil];
    googleDocsViewController.delegate = self;
    [self.navigationController pushViewController:googleDocsView animated:YES];
    [googleDocsView release]; 

I didn't know that why this code have warning. Warning like this.

Class 'RootViewController' does not implement the 'GoogleDocsViewControllerDelegate' protocol

What can I do ?

ADD SOURCE CODE

GoogleViewController.h

@protocol GoogleDocsViewControllerDelegate;
@interface GoogleDocsViewController : UIViewController<UITextFieldDelegate> {       
    id<GoogleDocsViewControllerDelegate> delegate;
}
@property (nonatomic,assign) id<GoogleDocsViewControllerDelegate> delegate;

@end

@protocol GoogleDocsViewControllerDelegate<NSObject>;

@required

-(void) googleViewControllerInputDidEnd:(NSString *)text;

@end

googleDocsViewController.m

-(void)googleViewControllerInputDidEnd:(NSString *)text{
    NSLog(@"delegate"); 
}

This googleViewControllerInputDidEnd:(NSString*)text method is test source.

解决方案

Can you tell us which class is creating the GoogleDocsViewController? Check that class' header (.h file) and see if it implements the GoogleDocsViewControllerDelegate.

Basically the delegate is the object which will listen to interesting events from the GoogleDocsViewController and it should implement the protocol mentioned so it "knows" which actions to perform when the events are fired.

这篇关于如何在iPhone上使用委托模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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