我搜索了SO,但是没有找到答案。
我在storyboard主UITableViewController中创建了一个名为A的简单按钮。和另一个名为B的ViewController,它有webView和close按钮。
它没有以任何形式连接到主UITableViewController A
现在我想打开B视图控制器窗体A,然后关闭B视图控制器与它自己的关闭按钮。
但无论如何我尝试B视图控制器是黑色和空白。
ViewController B(弹出视图)
#import "TAFBLoginDialogViewController.h"
@interface TAFBLoginDialogViewController ()
@end
@implementation TAFBLoginDialogViewController
-(id)initWithAppId:(NSString *)apiKey
requestPremision:(NSString *)requestPremision
delegate:(id<TAFBLoginDialogViewdelegate>)delegate
storyBoardName:(NSString*) storyBoardName
{
//self = [super initWithNibName:@"FBLoginDialog" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"FBLoginDialog"];
if (self) {
// Custom initialization
self.apiKey = apiKey;
self.requestPremision = requestPremision;
self.delegate = delegate;
}
return self;
}
- (IBAction)closeTap:(id)sender
{
[self.delegate closeTaped];
}
-(void)login
{
}
-(void)logout
{
}
-(void)checkForAccessToken:(NSString*)urlString
{
}
-(void)checkLoginRequired:(NSString*)urlString
{
[self.delegate displayRequired];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
UITableViewController A(主视图控制器,我试图打开B)
#import "TAFMETableViewController.h"
@interface TAFMETableViewController ()
{
}
@end
@implementation TAFMETableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void) awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cell;
}
- (IBAction)handleOpenFBDialog:(id)sender {
UIStoryboard * storyboard = self.storyboard;
NSString * storyboardName = [storyboard valueForKey:@"name"];
self.appId = @"11111";
self.permissions =@"public_stream";
if(_loginDialogView ==nil)
{
self.LoginDialogViewController = [[TAFBLoginDialogViewController alloc] initWithAppId:_appId
requestPremision:_permissions
delegate:self storyBoardName:storyboardName];
self.loginDialogView = _LoginDialogViewController.view;
}
[self.LoginDialogViewController checkLoginRequired:@"tst"];
NSLog(@"Click!");
}
-(void)accessTokenFound:(NSString*)accessToken
{
NSLog(@"accessTokenFound Click!");
}
-(void)displayRequired
{
NSLog(@"displayRequired Click!");
[self presentViewController:_LoginDialogViewController animated:YES completion:nil];
}
-(void)closeTaped
{
NSLog(@"closeTaped Click!");
[self dismissViewControllerAnimated:NO completion:nil];
}
@end
头文件:
@protocol TAFBLoginDialogViewdelegate
-(void)accessTokenFound:(NSString*)accessToken;
-(void)displayRequired;
-(void)closeTaped;
@end
@interface TAFBLoginDialogViewController : UIViewController<UIWebViewDelegate>
{
//ivars
// UIWebView *_webview;
// NSString* _apiKey;
// NSString* _requestPremision;
// id <TAFBLoginDialogViewdelegate> _delegate;
}
@property (retain) IBOutlet UIWebView *webView;
@property (copy) NSString *apiKey;
@property (copy) NSString *requestPremision;
@property (assign) id<TAFBLoginDialogViewdelegate> delegate;
-(id)initWithAppId:(NSString*)apiKey
requestPremision:(NSString*)requestPremision
delegate:(id<TAFBLoginDialogViewdelegate>)delegate
storyBoardName:(NSString*) storyBoardName;
- (IBAction)closeTap:(id)sender;
-(void)login;
-(void)logout;
-(void)checkForAccessToken:(NSString*)urlString;
-(void)checkLoginRequired:(NSString*)urlString;
@end
我在tableviewa中有一个按钮触发:
- (IBAction)handleOpenFBDialog:(id)sender
则此函数初始化ViewController B调用:
[self.LoginDialogViewController checkLoginRequired:@"tst"];
应该显示ViewController B但它只能显示黑屏。
在你的initWithAppID方法中,你从故事板实例化你的视图控制器,然后不对它做任何事情。然后你有一个if (self)初始化块但是你从来没有初始化过self。
看起来你想要做的是设置self等于你从storyboard实例化的视图控制器然后设置它的属性。
它可能使这个类方法更有意义,因为你分配一个视图控制器对象,在它的init方法中,你从storyboard中创建另一个,并忽略你分配的那个。
+(instancetype) TAFBLoginDialogViewControllerWithAppID:(NSString *)apiKey
requestPremision:(NSString *)requestPremision
delegate:(id<TAFBLoginDialogViewdelegate>)delegate
storyBoardName:(NSString*) storyBoardName
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
TAFBLoginDialogViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"FBLoginDialog"];
// Custom initialization
viewController.apiKey = apiKey;
viewController.requestPremision = requestPremision;
viewController.delegate = delegate;
return viewController;
}