我是Objective C的新手,我无法让代表在我的代码中工作。
我有一个名为cViewController的类,在这个类中有一个UIWebView来获取网页,上面有一个按钮,它调用一个名为prodMenu的popover类。我遇到的问题是,从prodMenu类调用webpages并在父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
}
我在这里错过了调用委托方法的内容吗?没有任何错误或警告。我想要做的就是当用户按下弹出窗口视图上的按钮时,下面的父类会更改网页。提前谢谢。
答案 0 :(得分:1)
只是一个简单的想法。您是否尝试过像这样声明委托协议
@protocol prodMenuDelegate <NSObject>
...
@end
?当省略&lt;时,我经常遇到麻烦。 NSObject&gt;。另外我认为
中缺少'id'@interface prodMenu : UIViewController{
<prodMenuDelegate> delegate;
}
编辑:此外,在您发布的代码中,我看不到您实际分配prodMenu代表的位置。