我是新来的Objective C,我无法让委托在我的代码中工作。从弹出窗口类调用委托方法
我有一个名为cViewController的类,在这个类中有一个UIWebView来获取网页和它上面的一个按钮,它调用一个名为prodMenu的弹出类。我遇到的麻烦是,从prodMenu类调用网页并将其显示在父UIWebView上的功能不起作用。
cViewController.h
#import <UIKit/UIKit.h>
#import "prodMenu.h"
@interface cViewController : UIViewController <prodMenuDelegate>{
UIPopoverController *cpopover;
IBOutlet UIWebView *webImageDisplay;
}
@property (nonatomic, retain) UIWebView *webImageDisplay;
@property (nonatomic, retain) UIPopoverController *cpopover;
- (IBAction)cPopoverTapped:(id)sender;
- (void) fetchWebsite:(NSString *)website; //This is the delegate method I'm trying to call in the popover class.
@end
cViewController.m
- (IBAction)cPopoverTapped:(id)sender {
prodMenu *tp = [[prodMenu alloc] init];
cpopover = [[UIPopoverController alloc] initWithContentViewController:tp];
[tp release];
CGRect rect = CGRectMake(40, 0, 50, 50);
cpopover.popoverContentSize = CGSizeMake(250, 225);
[cpopover presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (void)fetchWebsite:(NSString*)website{ //This function is called by prodMenu class
NSLog(@"address: %@", website);
NSString *urlAddress = website;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webImageDisplay loadRequest:requestObj];
[webImageDisplay release];
}
- (void)viewDidLoad {
[self fetchWebsite:@"http://www.yahoo.com"];
[super viewDidLoad];
}
prodMenu.h
#import <UIKit/UIKit.h>
@protocol prodMenuDelegate;
@interface prodMenu : UIViewController{
<prodMenuDelegate> delegate;
}
- (IBAction)radBtn:(id)sender; //This is the IBAction that calls the fetchWebsite function
@property (nonatomic, assign) id <prodMenuDelegate> delegate;
@end
@protocol prodMenuDelegate
- (void)fetchWebsite:(NSString *)website;
@end
prodMenu.m
#import "prodMenu.h"
#import "cViewController.h"
@implementation prodMenu
@synthesize delegate;
- (IBAction)radBtn:(id)sender{
[delegate fetchWebsite:@"http://www.google.com"]; //Here is the call to the method
}
我失去了一些东西在这里调用委托方法?没有任何错误或警告。我想要做的就是当用户在弹出视图上按下按钮时,下面的父类会更改网页。提前致谢。
prodMenu的实例在哪里?这就是将cViewController实例分配给“委托”属性的地方。 – 2011-03-08 15:11:10
对不起,添加了prodMenu的实例 – 2011-03-08 15:37:42