尝试将UIApplication子类化并在委托中实现它,但是被代理们搞糊涂了

时间:2012-12-13 15:17:05

标签: objective-c ios delegates subclass

我试图按照How to intercept click on link in UITextView?中接受的回答中的建议,但我设法让自己感到非常困惑,部分原因是因为我不太了解代表团,现在我已经添加和删​​除了很多东西,我不知道发生了什么。

这是GHFeedback.h

#import <UIKit/UIKit.h>
#import "GHApplicationSubclassed.h"

@interface GHFeedback : UIViewController <UIApplicationDelegate>

@property (nonatomic, strong) UITextView *feedbackInstructions;
@property (nonatomic, strong) GHApplicationSubclassed *ghAppSub;

@end

这是GHFeedback.m

#import "GHFeedback.h"
#import <MessageUI/MessageUI.h>
#import "GHApplicationSubclassed.h"

@interface GHFeedback () <UIApplicationDelegate, MFMailComposeViewControllerDelegate>

@end

@implementation GHFeedback

@synthesize feedbackInstructions, ghAppSub;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.ghAppSub.delegate=self;
}
return self;
}

-(void)openURL:(NSURL *)url //This is the method I'm trying to add--the text set for self.feedbackInstructions in IB contains an email address, and I want a subject line to appear automatically if the user sends an email.
{
    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    mailer.mailComposeDelegate = self;
    [mailer setSubject:@"feedback on Haiku"];
    [self presentViewController:mailer animated:YES completion:NULL];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这是GHApplicationSubclassed.h

#import <UIKit/UIKit.h>

@interface GHApplicationSubclassed : UIApplication <UIApplicationDelegate>

@property (nonatomic, strong) GHApplicationSubclassed *appli;

@end

这是GHApplicationSubclassed.m

#import "GHApplicationSubclassed.h"

@implementation GHApplicationSubclassed

@synthesize appli;

-(BOOL)openURL:(NSURL *)url
{
    if  ([self.delegate openURL:url]) //This line gets an error, "no known instance method for selector 'openURL'
        return YES;
    else
        return [super openURL:url];
}

@end

我喜欢关于如何解决这个问题的明确指示。 (通过“显式”我的意思是,而不是说类似的东西,“然后实现委托方法”,你可以说,“然后将此方法添加到GHFeedback.m-(void)openURL {[actual methods, etc., etc.]}

非常感谢你的帮助。

编辑:我想要发生的是这个。

UITextView的视图控制器中显示GHFeedback,表示“如果您对该应用有任何疑问/问题,请给我发电子邮件”,然后提供我的电子邮件地址。现在,当用户按下该电子邮件地址时,iOS Mail程序会打开一个空的草稿电子邮件。我想给草稿电子邮件打开“Haiku反馈”的自动主题行。

1 个答案:

答案 0 :(得分:0)

if  ([self.delegate openURL:url]) 

openURL的返回类型:void,因此实际上没有任何内容可以检查if语句。

也许你想用

if ([(GHFeedback *)self.delegate respondsToSelector:@selector(openURL:)]{
    [(GHFeedback *)self.delegate openURL:url]; 
    return YES;
}

如果仍然无法触发正确的openURL:,则self.delegate必须为零。