2017-04-05 19 views
0

私はHomeControllerに設定したUIラベルを更新するのにUICollectionViewdidSelectItemAtを使用しています。だから基本的に私がセルを押すと、私は自分のテキストを更新したい。ただし、テキストは更新されませんが、私はデバッグして値を変更しています。私は、main thread通じ両方setNeedsDisplay()やランニングを(それがすでにん)新しい値があってもUILabelのテキストは更新されません。すべて試しました

私はこれらのクラスのコードのかなり多くを持っている(私はストーリーボードを使用していない)が、これはラベルの設定であるが、(中に座っているすべてのものを試してみましたにHomeController):

import UIKit 
import CoreData 

class mainHomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 
static let sharedInstance = HomeController() 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     setupBasketBar() 

    } 

    let totNumber: UILabel = { 
     let label = UILabel() 
     label.text = "0" 
     label.numberOfLines = 2 
     return label 
    }() 

    func setupBasketBar() { 

     self.view.addSubview(totNumber) 
     totNumber.translatesAutoresizingMaskIntoConstraints = false 
     totNumber.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 330).isActive = true 
     totNumber.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true 
     totNumber.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: 0).isActive = true 
     totNumber.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 5).isActive = true 


    } 

} 

と、これはフィードセルに座っている:私は関連を編集した

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    HomeController.sharedInstance.totNumber.text = ("234") 
    HomeController.sharedInstance.totNumber.setNeedsDisplay() 


} 

部品。

これはにHomeControllerのために全体のコードです:

import UIKit 
import CoreData 

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    static let sharedInstance = HomeController() 

    let cellId = "cellId" 
    let trendingCellId = "trendingCellId" 
    let subscriptionCellId = "subscriptionCellId" 

    let titles = ["Home", "Trending", "Subscriptions", "Account"] 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     navigationController?.navigationBar.isTranslucent = false 

     let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: view.frame.height)) 
     titleLabel.text = "Home" 
     titleLabel.textColor = UIColor.black 
     titleLabel.font = UIFont.systemFont(ofSize: 20) 
     navigationItem.titleView = titleLabel 

     setupCollectionView() 
     setupMenuBar() 
     setupBasketBar() 

    } 

    func setupCollectionView() { 
     if let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout { 
      flowLayout.scrollDirection = .horizontal 
      flowLayout.minimumLineSpacing = 0 
     } 

     collectionView?.backgroundColor = UIColor.white 
     collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: cellId) 
     collectionView?.register(TrendingCell.self, forCellWithReuseIdentifier: trendingCellId) 
     collectionView?.register(SubscriptionCell.self, forCellWithReuseIdentifier: subscriptionCellId) 


     collectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0) 
     collectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(50, 0, 0, 0) 

     collectionView?.isPagingEnabled = true 
    } 

    lazy var settingsLauncher: SettingsLauncher = { 
     let launcher = SettingsLauncher() 
     launcher.homeController = self 
     return launcher 
    }() 

    func handleMore() { 
     //show menu 
     settingsLauncher.showSettings() 
    } 

    func showControllerForSetting(_ setting: Setting) { 
     let dummySettingsViewController = UIViewController() 
     dummySettingsViewController.view.backgroundColor = UIColor.white 
     dummySettingsViewController.navigationItem.title = setting.name.rawValue 
     navigationController?.navigationBar.tintColor = UIColor.white 
     navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
     navigationController?.pushViewController(dummySettingsViewController, animated: true) 
    } 

    func handleSearch() { 
     scrollToMenuIndex(2) 

    } 

    func scrollToMenuIndex(_ menuIndex: Int) { 
     let indexPath = IndexPath(item: menuIndex, section: 0) 
     collectionView?.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition(), animated: true) 

     setTitleForIndex(menuIndex) 
    } 


    fileprivate func setTitleForIndex(_ index: Int) { 
     if let titleLabel = navigationItem.titleView as? UILabel { 
      titleLabel.text = " \(titles[index])" 
     } 

    } 

    lazy var menuBar: MenuBar = { 
     let mb = MenuBar() 
     mb.homeController = self 
     return mb 
    }() 

    func buttonAction(sender: UIButton!) { 
     let btnsendtag: UIButton = sender 
     if btnsendtag.tag == 1 { 

      let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 

      let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "NewViewController") as! UINavigationController 

      self.present(vc, animated: true, completion: nil) 

     } 

    } 

    let totLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21)) 
    let btn: UIButton = UIButton(frame: CGRect(x: 0, y: 600, width: 500, height: 80)) 

    func setupBasketBar() { 

     //Checkout button 
     btn.backgroundColor = UIColor.rgb(36, green: 51, blue: 70) 

     btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) 
     btn.tag = 1 
     self.view.addSubview(btn) 
     btn.setTitleColor(UIColor.yellow, for: .normal) 

     //Button that updates the current amount 

     totLabel.text = "Total amount" 
     totLabel.textColor = UIColor.white 
     self.view.addSubview(totLabel) 

     //constraints 

     totLabel.translatesAutoresizingMaskIntoConstraints = false 
     totLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true 
     totLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true 
     totLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: 0).isActive = true 
     totLabel.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1).isActive = true 


     //oldconstraints 

     /* self.view.addSubview(totNumber) 
     totNumber.translatesAutoresizingMaskIntoConstraints = false 
     totNumber.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 330).isActive = true 
     totNumber.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true 
     totNumber.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: 0).isActive = true 
     totNumber.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1).isActive = true*/ 


    } 

    fileprivate func setupMenuBar() { 
     navigationController?.hidesBarsOnSwipe = false 

     let redView = UIView() 
     redView.backgroundColor = UIColor.rgb(36, green: 51, blue: 70) 
     view.addSubview(redView) 
     view.addConstraintsWithFormat("H:|[v0]|", views: redView) 
     view.addConstraintsWithFormat("V:[v0(50)]", views: redView) 

     view.addSubview(menuBar) 
     view.addConstraintsWithFormat("H:|[v0]|", views: menuBar) 
     view.addConstraintsWithFormat("V:[v0(50)]", views: menuBar) 


     menuBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true 
    } 


    override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { 

     let index = targetContentOffset.pointee.x/view.frame.width 

     let indexPath = IndexPath(item: Int(index), section: 0) 
     menuBar.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally) 

     setTitleForIndex(Int(index)) 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 5 

    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let identifier: String 
     if indexPath.item == 1 { 
      identifier = trendingCellId 
     } else if indexPath.item == 2 { 
      identifier = subscriptionCellId 
     } else { 
      identifier = cellId 
     } 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) 

     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: view.frame.width, height: view.frame.height - 50) 
    } 

} 
+0

可能な重複[スウィフトとシンプルなコレクションビューを作る方法](http://stackoverflow.com/questions/31735228/how-to -make-a-simple-collection-view-with-swift) –

+0

ようこそstackoverflowへようこそ!私はシングルトンパターン( 'sharedInstance'を使って)を使うアプローチは、あなたが達成しようとしているものに対して、より複雑で不必要なアプローチであることはほとんど確信しています。 'mainHomeController'はタイプミスか、プロジェクトに' mainHomeController'と 'HomeController'クラスの両方が含まれていますか? 'didSelectItemAt'メソッドは' HomeController'または 'mainHomeController'で呼び出されていますか?それはたくさんのように見えるかもしれませんが、クラス全体を貼り付けると、多分助けになるでしょう。 – MathewS

+0

ありがとう@MathewS!私はコーディングにも新しいものが新しくなっています。私が達成したいことはもっと簡単にするべきだと思います...はい、ただ1つの 'HomeController'しかありません - 私はコードをきれいにしたいと思っていたのでタイプミスでした。 私は上記の 'HomeController'のためにすべてのコードをアップロードしました。基本的に 'JSON'を通して' collectionViewCells'に '文字列配列 'を送ります。次に、ボタンを使って配列データをセルから 'HomeController''UILabel'に送信します。 – chetbaker

答えて

0

私はあなたのsharedInstance変数は、ビューコントローラの新しいインスタンスで推測している、これはストーリーボードやから割り当てられ、表示される1から分離されていますセグ。 sharedInstanceselfと同じに設定する必要がありますviewDidLoad

+0

はい、私もそう思います。 'SharedInstance'は新しいインスタンスであり、' self'とは異なるメモリロケーションを持っています。 – ron27

+0

ありがとう!私は何かが共有インスタンスに間違っていた疑いがありました。 viewDidLoadでsharedInstanceをselfに設定するにはどうすればよいですか?私はコーディングに新しいです、本当にそれを理解することはできません。 – chetbaker

1

大丈夫です!更新されたコードスニペットを見ると、sharedInstanceシングルトンは間違いなく必要ありません。

didSelectItemAt方法は、ちょうどこのする必要があります:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    totNumber.text = "234" 
} 
関連する問題