gpt4 book ai didi

ios - 如何使用框架调用 UIViewController 类

转载 作者:行者123 更新时间:2023-11-29 01:19:42 25 4
gpt4 key购买 nike

我已经使用此创建了一个静态库/框架tutorial

为了调用 View 类 customView **(类型 UIView )**我们使用下面的代码

-(void) showMessageInViewController:(UIViewController *)viewController {
if (_isEnabled) {
NSBundle* frameworkBundle = [NSBundle bundleForClass:[self class]];
CustomView *csView = [[frameworkBundle loadNibNamed:@"CustomView" owner:self options:nil] firstObject];
csView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
[viewController.view addSubview:csView];
}
}

我的问题是:

如果我在我的框架内创建一个 UIViewController 类然后我如何使用我的类导航或调用它

-(void)showMessageInUIViewController:(UIViewController *)uiviewController
{
DetailViewController *viewController = [[DetailViewController alloc]
initWithNibName:@"DetailViewController"
bundle:nil];
viewController.delegate = self;
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:viewController];
[self presentModalViewController:navController animated:YES];
}

它在 .delegate 和presentModalViewController 中显示错误

最佳答案

您可以使用对您当前使用的框架的 UIViewController 的引用。在 InsertManager.h

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

@interface InsertManager : NSObject

+(instancetype) sharedManager;
-(void) setViewController; // the view controller will use it to assign itself to the framework.
-(void) startManager;
-(void) stopManager;

-(void) showMessageInViewController:(UIViewController *)viewController;

-(BOOL) isManagerRunning;

@end

InsertManager.m

#import "InsertManager.h"
#import "CustomView.h"

@interface InsertManager()

@property (nonatomic) BOOL isEnabled;
@property (readOnly) BOOL hasViewController;
@property (nonatomic, weak) CustomView * csView;

@end

@implementation InsertManager

+ (instancetype) sharedManager {
static InsertManager *sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[[self class] alloc] init];
// create the custom view only once.
NSBundle* frameworkBundle = [NSBundle bundleForClass:[self class]];
_csView = [[frameworkBundle loadNibNamed:@"CustomView" owner:self options:nil] firstObject];
csView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
});
return sharedManager;
}
-(Bool) hasViewController {
return _viewController != nil;
}

- (void) setViewController:(UIViewController *) viewController {
_viewController = viewController
// add the views you want to it all at once, but keep them hidden.
_csView.hidden = YES;
[_viewController.view addSubview:csView];
}
- (void) startManager {
NSLog(@"Manager is running");
_isEnabled = YES;
}

- (void) stopManager {
NSLog(@"Manager stopped..");
_isEnabled = NO;
}

-(BOOL) isManagerRunning {
return _isEnabled;
}

-(void) showMessage {
if (_isEnabled && _hasViewController) {
// Only unhide the custom view as its already added to the _viewController and hidden.
_csView.hidden = NO;
}
}

@end

自定义 View .m

  #import "CustomView.h"

@implementation CustomView

- (IBAction)closeButtonClicked:(id)sender {
// [self removeFromSuperview];
self.hidden = YES;
}
@end

希望对您有所帮助。它可能需要一些调整,因为我刚刚在这里写了它(没有编译器)。谢谢。

关于ios - 如何使用框架调用 UIViewController 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34762900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com