Search code examples
objective-cios7xcode5xcode-storyboard

presentViewController with storyboards shows black view iOS 7.1 xcode 5.1


i did search SO and the web didn't find amy answer .
i created in storyboard main UITableViewController called A which have simple button. and another ViewController Called B which have webView and close button.
that is not connected in any form to the main UITableViewController A
now i like to open the B viewController form A and then close the B ViewController with its own close button .
but what ever i try the B view controller is black and blank .

ViewController B ( the popup view )

#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 ( the main view controller from which i try to open 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

header file :

@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

I have button in the TableView A that trigger :

- (IBAction)handleOpenFBDialog:(id)sender  

then this function init the ViewController B and the invoking :

[self.LoginDialogViewController checkLoginRequired:@"tst"];

which supposed to show the ViewController B but all it does shows black screen .


Solution

  • in your initWithAppID method you instantiate your view controller from the storyboard and then don't do anything with it. Then you have an if (self) initialization block but you've never initialized self.

    It looks like what you want to do is set self equal to the view controller that you instantiated from the storyboard and then set the properties on it.

    It might make more sense to make this a class method instead since your allocating a view controller object and inside it's init method your creating another one from the storyboard and ignoring the one you allocated.

    +(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; 
    }