带有情节提要的 presentViewController 显示黑色视图 iOS 7.1 xcode 5.1
Posted
技术标签:
【中文标题】带有情节提要的 presentViewController 显示黑色视图 iOS 7.1 xcode 5.1【英文标题】:presentViewController with storyboards shows black view iOS 7.1 xcode 5.1 【发布时间】:2014-07-26 06:22:38 【问题描述】:我确实搜索过,但网上没有找到艾米的答案。 我在故事板主 UITableViewController 中创建了一个名为 A 的简单按钮。 和另一个名为 B 的 ViewController 具有 webView 和关闭按钮。 没有以任何形式连接到主 UITableViewController A 现在我喜欢打开 B viewController form A 然后用它自己的关闭按钮关闭 B ViewController。 但是我尝试过的 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
我在 TableView A 中有触发按钮:
- (IBAction)handleOpenFBDialog:(id)sender
然后这个函数初始化 ViewController B 和调用:
[self.LoginDialogViewController checkLoginRequired:@"tst"];
应该显示 ViewController B 但它所做的一切都显示黑屏。
【问题讨论】:
【参考方案1】:在您的 initWithAppID 方法中,您从情节提要中实例化您的视图控制器,然后不对它做任何事情。然后你有一个 if (self) 初始化块,但你从来没有初始化过 self。
看起来您想要做的是将 self 设置为等于您从情节提要中实例化的视图控制器,然后在其上设置属性。
将其设为类方法可能更有意义,因为您分配了一个视图控制器对象,并且在它的 init 方法中您从情节提要创建了另一个并忽略了您分配的那个。
+(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;
【讨论】:
感谢您的回答,我收到了这些错误,我想我没有正确设置 apiKey 和所有其他属性:在 'UIViewController *' 类型的对象上找不到属性 'apiKey' 属性 'requestPremision'在“UIViewController *”类型的对象上找不到我更新了问题 抱歉,在我的示例中,我将 viewController 声明为 UIViewController 而不是 TAFBLoginDialogViewController,这就是为什么您会收到属性不存在的错误。我已经更新了示例。以上是关于带有情节提要的 presentViewController 显示黑色视图 iOS 7.1 xcode 5.1的主要内容,如果未能解决你的问题,请参考以下文章
带有自定义单元格的 ViewController 内的 UITableView 没有情节提要