2016-01-13 81 views
0

我一直在使用这个tutorial如何使用框架

用于调用视图类customView **(类型的UIView)创建一个静态库/框架调用UIViewController类**我们用下面的代码

-(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

+0

创建'UIViewController'没有意义,因为这个视图控制器不会在你的应用程序的层次结构中。它要么将当前视图控制器作为参数发送(如同在第一部分中那样)或使用委派。 – Ismail

+0

意味着使用框架视图我总是要创建UIView?总是我必须将它们添加为子视图? – iphonemaclover

+0

本教程中的方式对我来说很奇怪。您可以避免每次只创建一次然后使用它来创建视图。您也可以避免将其添加为子视图,只需添加一次并隐藏它,而不是每次都将其删除。 – Ismail

回答

1

您可以使用还得你使用你的框架目前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 

CustomView.m

#import "CustomView.h" 

    @implementation CustomView 

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

我希望这有助于。它可能需要一点调整,因为我在这里写了它(没有编译器)。谢谢。