ios - 创建类似于 Extension Swift 的 UIViewController 类别(Obj-C)
<p><p>我正在尝试使用一些自定义方法扩展标准 <code>UIViewController</code>。</p>
<pre><code>#import <UIKit/UIKit.h>
@interface UIViewController (UIViewControllerExtension)
- (void) showNoHandlerAlertWithTitle:(NSString *)title andMessage: (NSString*)message;
- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString*)messagebuttonTitles:(NSArray<NSString *>*)titles andHandler:(void (^)(UIAlertAction * action))handler;
@end
</code></pre>
<p>我现在如何使用扩展的 <code>UIViewController</code>?我需要从扩展的 <code>UIViewController</code> 继承我的自定义 ViewController 。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>创建一个文件“UIViewController+Alert.h”,其中包含:</p>
<pre><code>#import <UIKit/UIKit.h>
@interface UIViewController (AlertExtension)
- (void) showNoHandlerAlertWithTitle:(NSString *)title andMessage: (NSString*)message;
- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString*)messagebuttonTitles:(NSArray<NSString *>*)titles andHandler:(void (^)(UIAlertAction * action))handler;
@end
</code></pre>
<p>然后,创建一个文件“UIViewController+Alert.m”,其中包含:</p>
<pre><code>#import "UIViewController+Alert.h"
@implementation UIViewController (AlertExtension)
- (void) showNoHandlerAlertWithTitle:(NSString *)title andMessage: (NSString*)message {
// Insert code here
}
- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString*)messagebuttonTitles:(NSArray<NSString *>*)titles andHandler:(void (^)(UIAlertAction * action))handler {
// Insert code here
}
@end
</code></pre>
<p>说你的“SampleViewController.h”:</p>
<pre><code>#import <UIKit/UIKit.h>
@interface SampleViewController : UIViewController
@end
</code></pre>
<p>然后在“SampleViewController.m”中:</p>
<pre><code>#import "SampleViewController.h"
#import "UIViewController+Alert.h"
@implementation SampleViewController
- (void)viewDidLoad {
;
;
}
@end
</code></pre></p>
<p style="font-size: 20px;">关于ios - 创建类似于 Extension Swift 的 UIViewController 类别(Obj-C),我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/46217694/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/46217694/
</a>
</p>
页:
[1]