代码之家  ›  专栏  ›  技术社区  ›  user63898

带有故事板的presentViewController显示黑色视图iOS 7.1 xcode 5.1

  •  0
  • user63898  · 技术社区  · 10 年前

    我搜索了SO,但网络上没有找到amy的答案。
    我在故事板中创建了名为A的UITableViewController,它有一个简单的按钮。 另一个名为B的ViewController具有webView和关闭按钮。
    未以任何形式连接到主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
    

    我在TableView A中有一个按钮可以触发:

    - (IBAction)handleOpenFBDialog:(id)sender  
    

    然后该函数初始化ViewController B 以及调用:

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

    它应该显示ViewController B 但它显示的都是黑屏。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Brandon    10 年前

    在initWithAppID方法中,您从故事板实例化视图控制器,然后不对其进行任何操作。然后您有一个if(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; 
    }