首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用框架调用UIViewController类

如何使用框架调用UIViewController类
EN

Stack Overflow用户
提问于 2016-01-13 09:31:12
回答 1查看 871关注 0票数 0

我已经使用这个/Framework创建了一个静态库教程

为了调用视图类customView **( UIView类型)**,我们使用了以下代码

代码语言:javascript
运行
复制
-(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类,那么我是如何使用类导航或调用它的

代码语言:javascript
运行
复制
-(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中的错误

EN

回答 1

Stack Overflow用户

发布于 2016-01-13 10:41:32

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

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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 

CustomView.m

代码语言:javascript
运行
复制
  #import "CustomView.h"

    @implementation CustomView

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

我希望这能帮到你。它可能需要一些调整,因为我刚才写在这里(没有强者)。谢谢。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34762900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档