在Swift上以编程方式创建视图控制器时出现问题

时间:2016-05-31 00:32:10

标签: ios swift uiviewcontroller uinavigationcontroller

我是swift的新手,我正在尝试在不使用故事板的情况下构建应用程序。该应用程序实际上是我已经在Objective C中创建的应用程序,并且我试图在swift中进行复制。

我遇到了视图控制器和导航控制器的一些问题。当应用程序启动时,没有问题,但是当它从初始视图推送到下一个视图时,导航控制器会发生奇怪的事情。看起来主要的下面有第二个导航控制器。以下是更改original next view的视图图片。如您所见,顶部有一个奇怪的酒吧。

以下是该app代理的代码,用于创建第一个视图和推送第二个视图的代码

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
    let rootView = EnterTwitterHandleViewController();
    let navBar = UINavigationController(rootViewController: rootView);
    let tab = UITabBarController();
    tab.viewControllers = [navBar];
    self.window?.addSubview(tab.view);
    self.window?.rootViewController = tab;
    self.window!.makeKeyAndVisible()

以及推送到第二个

的代码
        let vc = TweetFeedTableViewController(style: UITableViewStyle.Grouped)
        vc.arrayWithTweets = self.userFeedArray;
        let title = NSString(format: "%@'s Tweets", self.userName.nameOfUser);
        vc.title = title as String;

        self.navigationController?.pushViewController(vc, animated: true);

此外,顶部应该有一个搜索控制器,但没有一个。这是搜索控制器的代码

  self.searchController = UISearchController(searchResultsController: nil);
    self.searchController.loadViewIfNeeded();
    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.searchController.dimsBackgroundDuringPresentation = false;
    self.searchController.searchBar.backgroundColor = UIColor.whiteColor();
    self.searchController.delegate = self;
    self.searchController.searchBar.delegate = self;

最后,当我从第二个视图推到第三个视图时,会出现黑屏。我记录了正在发送的数据并且它看起来正确,所以我的猜测是这与视图的初始化有关。这是推送第三个的代码,这是第三个view的屏幕截图。

 let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCell;
    print(cell.tweet.message);
    let vc = TweetDetailViewController();
    vc.tweet = cell.tweet;

    self.navigationController?.pushViewController(vc, animated: true);

好吧,我玩了代码,现在,视图控制器中的一个元素 - 图像视图 - 显示出来,但其余的元素都是空白的,即使我设置了背景,屏幕仍然是黑色的是一种特定的颜色。以下是如何创建UI的代码。这里是显示内容和视图调试器的图像。 viewdebuggerdebugger 2

    override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated);

    self.view.backgroundColor = twitterBlue;

    let title = NSString(format: "%@'s Tweet", self.tweet.user.nameOfUser);
    self.title = title as String;


    self.navigationController?.navigationBar.barTintColor = tweetGray;

    self.navigationController?.navigationBar.translucent = false;
    self.navigationController?.navigationBar.tintColor = twitterBlue;
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:twitterBlue];

    createUI();
}


func createUI() {

    let size = self.view.frame.size;

    //heart

    let messageView = CGRectMake(25, size.width-180, size.width-50, 260)
    self.messageLabel = UILabel(frame: messageView);
    self.messageLabel.text = tweet.message;
    self.messageLabel.textColor = tweetGray;
    self.messageLabel.font = UIFont(name: "Avenir", size: 27);
    self.messageLabel.textAlignment = NSTextAlignment.Center;
    self.messageLabel.numberOfLines = 0;
    self.view.addSubview(self.messageLabel);

    let d = self.tweet.dateOfTweet;
    let formatter = NSDateFormatter();
    formatter.dateFormat = "MMMM dd, YYYY HH:mm";
    let resultString = formatter.stringFromDate(d);

    let dateView = CGRectMake(25, (size.width + 105), (size.width - 50), 40);
    self.dateLabel = UILabel(frame: dateView);
    self.dateLabel.text = resultString;
    self.dateLabel.textColor = tweetGray;
    self.dateLabel.textAlignment = NSTextAlignment.Center;
    self.dateLabel.font = UIFont(name: "Avenir", size: 18);
    self.view.addSubview(self.dateLabel);

    var imageOfUser = UIImage();
    let picView = CGRectMake(100, 20, (size.width - 200), (size.width - 200));
    self.profilePictureImage = UIImageView(frame: picView);
    self.profilePictureImage.layer.cornerRadius = 10;
    self.profilePictureImage.layer.masksToBounds = true;
    self.view.addSubview(self.profilePictureImage);

    if (tweet.user.profilePicture != nil) {
        imageOfUser = UIImage(data: tweet.user.profilePicture)!;
        self.profilePictureImage.image = imageOfUser;
        self.profilePictureImage.layer.borderColor = UIColor.whiteColor().CGColor;
        self.profilePictureImage.layer.borderWidth = 8;

    } else {

        let image2 = UIImage(named: "tweet");
        self.profilePictureImage.image = image2;
        var image: UIImage!

        let configuration : NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration();
        let session = NSURLSession(configuration: configuration);

        let url = NSURL(string:tweet.user.profilePictureUrl);

        let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) in

            image = UIImage(data: data!);

            dispatch_async(dispatch_get_main_queue(), { 

                if (image != nil) {
                    self.profilePictureImage.image = image;
                    self.profilePictureImage.layer.borderColor = UIColor.whiteColor().CGColor;
                    self.profilePictureImage.layer.borderWidth = 8;
                }
            })
        })
        task.resume();
    }




}

enter code here

0 个答案:

没有答案