3

我在想,我怎样才能为 twitter 或 facebook 或任何东西开设课程。所以只要我将该类导入我的项目,它就开始使用一个代码。例如。如果我使用 twitter 框架,那么我必须编写它的功能。我只想通过导入它来创建一个类,它将在我未来的所有项目中运行。例子:

任何课程或任何东西:

-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image
{
   // ALL TWITTER Code HERE;
}

在我的任何项目的主视图控制器中:

- (IBAction)socialBtn:(id)sender
{
  [self shareOnTwitter:@"This is Text" withUrl:nil withImage:nil];
}

更新:刚刚尝试了分类方式,但没有响应返回,知道我错在哪里了吗?:

UIViewController+CustomMethods.h

#import <UIKit/UIKit.h>
#import <Social/Social.h>

@interface UIViewController (CustomMethods)

-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image;

@end

UIViewController+CustomMethods.m

#import "UIViewController+CustomMethods.h"

@implementation UIViewController (CustomMethods)

-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image {

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {

        SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        [tweetSheet setInitialText:text];
        [tweetSheet addImage:image];
        [tweetSheet addURL:url];

        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry"
                                  message:@"You can't Post a Status right now, make sure your device has an internet connection and you have at least one Twitter account setup"
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }

}
@end

主视图控制器:

NSString *title = [webViewOutlet stringByEvaluatingJavaScriptFromString:@"document.title"];
            NSURL *url = [[webViewOutlet request] URL];

            [self shareOnTwitter:title withUrl:url withImage:nil];
4

4 回答 4

4

我相信这是一个实例,您可以使用称为“类别”的 Objective-C 功能。类别是对已经建立的类的扩展。例如,我们可以为 UIViewController 创建一个类别,它将向所有 UIViewController 类添加新方法。

为此,我们执行以下操作:

制作一个声明原型的 .h 文件:

@interface UIViewController (CategoryName) // EG. (Twitter)
// PROTOTYPES... EG:
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image;
@end

以及实现它们的相应 .m 文件:

@implementation UIViewController (CategoryName)
// IMPLEMENTATIONS... EG:
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image {
    // Code...
}
@end

(请注意,当您在 XCode 中声明一个新文件时,它有一个您可以使用的“类别”模板。)

现在,每当您导入 .h 文件时,作为一个类的 UIViewController 将包含您扩展它的方法!

这是一个关于我通过快速谷歌搜索找到的类别的教程。

于 2013-03-30T05:30:41.093 回答
2

创建您的此类NSObject

@interface YourClassName : NSObject
.
.
. // put all methods and ivar..etc that your need. (in Proper way)
.
.

并将所有方法放入其中(方法体.m file

#import "YourClassName.h"在你想继承它的地方。

并且您可以访问放在课堂上的所有功能。YourClassName

于 2013-03-30T05:27:29.507 回答
1

要创建一个委托,您可以将其添加到接口声明之上YourTwitterClass.h

@protocol YourTwitterDelegate <NSObject>
-(void)delegteMethod;
@end

你会通过子类化来做这样的事情NSObject

//In YourTwitterClass.h
//Twitter framework(needed but I don't know the name)
#import <Twitter/Twitter.h>
#import <UIKit/UIKit.h>

@interface YourTwitterClass : NSObject

@property (nonatomic) id<YourTwitterDelegate> delegate;    
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image delegate:(id<YourTwitterDelegate>)del;

@end


//in YourTwitterClass.m
@implementation YourTwitterClass
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image delegate:(id<YourTwitterDelegate>)del{
_delegate = del;
[_delegate delegateMethod];//calls the delegate method
//do twitter stuff
}

然后在您的视图控制器中

//import "YourTwitterClass.h"

- (IBAction)socialBtn:(id)sender
{
YourTwitterClass *t = [[YourTwitterClass alloc] init];
[t shareOnTwitter:@"This is Text" withUrl:nil withImage:nil];
}
//and the delegate method
-(void)delegateMethod{
NSLog(@"This is a delegate method!);
}

在您的ViewController.h文件中,您还必须将其添加到接口声明中

编辑 如果你想添加一个代表,一个邮件撰写者,添加那个类的代表

@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>
//or
@interface YourTwitterClass : NSObject <MFMailComposeViewControllerDelegate>
于 2013-03-30T05:27:16.317 回答
0

答案使它比实际情况更复杂:

您需要使用 mytweet.h 和 mytweet.m 创建一个如上所述的 NSObject 类

在您的 UIViewController 方法中,您需要实例化您创建的自定义类:

#import Mytweet.h;

……

 -(void) viewDidload { ...

    }

然后调用方法如下:

 -(IBACTION) Handletweet{ 

      Mytweet *a = [[Mytweet alloc] init];
      Bool ABC = [Mytweet custom_method1:parameter1 p2:parameter2];}

as with anything in Objective C you will need to keep in mind that the objects has a lifetime it get created and released (automatically with ARC) so any parameter that you need to be live beyond the life of your custom class need to be created outside the custom class and passed as a parameter.

      Mytweet *a = [[Mytweet alloc] init];
      a.parameter1 = value;

where in my tweet.h

     @property (nonatomic,strong) NSString *parameter1;

Re: Delegates - Delegate methods are a way to override the written methods by a calling class, these are typical in Apple or 3rd party framework classes since the entire source may not be open to you, For custom classes, you don't need them just customize and add methods to the custom class as much as you want, you will find this results in cleaner code than otherwise

于 2013-03-30T07:45:23.433 回答