简体   繁体   English

仅在iPhone 4S上的postNotification之后添加观察者

[英]Observer being added after postNotification ONLY on iPhone 4S

I have a fully functional code that works perfectly on iOS >8.0 for iPhones >5 and all iPads, where I pass information and call function via Observers/Notifications, but on iPhone 4S it just doesn't work. 我有一个功能齐全的代码,可以在iOS> 8.0(适用于iPhone> 5的iPhone和所有iPad)上完美运行,在这里我可以通过Observers / Notifications传递信息和调用功能,但是在iPhone 4S上却无法正常工作。

Through debugging, I actually found out that ONLY while running on iPhone 4S the observer gets added AFTER the notification gets posted. 通过调试,我实际上发现, 只有在iPhone 4S上运行时,观察者是在通知发布添加的。

This is happening on devices and on the simulator, on iOS 8 and 9, respectively. 这分别在iOS 8和iOS 9的设备和模拟器上发生。

Code: 码:

**PostNotification**
override func viewDidLoad() {
    super.viewDidLoad()

    self.registerCells()

    UIApplication.sharedApplication().statusBarStyle = .LightContent

    self.collectionView.delaysContentTouches = false

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "footerUpdateContentSize:", name: "footerUpdateContentSize", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "seasonUpdateContentSize:", name: "seasonUpdateContentSize", object: nil)

    self.loadDetailTVShow()
}

func registerCells()
{
    self.collectionView.registerClass(HeaderDetailSeriesCell.self, forCellWithReuseIdentifier: "HeaderDetailSeriesCell")
    self.collectionView.registerClass(FooterDetailSeriesCell.self, forCellWithReuseIdentifier: "FooterDetailSeriesCell")
    self.collectionView.registerClass(SeriesSeasonContentCell.self, forCellWithReuseIdentifier: "SeriesSeasonContentCell")
}


func loadDetailTVShow()
{
    let id: String! = (tvShow != nil) ? tvShow!.id! : episode!.seriesId!

    ContentsClient().getContentById(id!).then { data -> Void in
        let m: TVShow? = data as! TVShow?
        self.tvShow = m!
        self.collectionView.reloadData()

        NSNotificationCenter.defaultCenter().postNotificationName("loadedTvShowlist", object: self.tvShow)

        UIView.animateWithDuration(1.0, delay: 0, options: .TransitionNone, animations:
            {
                self.loadingView.alpha = 0.0

            }, completion:nil)
    }
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    cell = collectionView.dequeueReusableCellWithReuseIdentifier("SeriesSeasonContentCell", forIndexPath: indexPath)

    let seriesContent = cell as! SeriesSeasonContentCell

    seriesContent.seriesContentCell?.tvShow = self.tvShow

    seriesContent.contentView.frame = CGRect(x: 0.0, y: 0.0, width: width, height: heightSeason)
    seriesContent.seriesContentCell?.view.frame = seriesContent.contentView.frame
}

**Observer**

class SeriesSeasonContent

override func viewDidLoad() {
    super.viewDidLoad()

    self.registerCells()

    seasonSelectedIndex = 0

    collectionView.reloadData()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadedTvShowlist:", name: "loadedTvShowlist", object: nil)
}

func registerCells ()
{
    self.seasonCollection.registerClass(SeasonViewCell.self, forCellWithReuseIdentifier: "SeasonViewCell")

    self.collectionView.registerClass(EpisodeViewCell.self, forCellWithReuseIdentifier: "EpisodeViewCell")
}

func loadedTvShowlist(notification : NSNotification){
    self.tvShow = notification.object! as? TVShow

    if (tvShow?.seasons != nil && tvShow?.seasons?.count > 0)
    {
        self.currentSeason = ((tvShow?.seasons!.objectAtIndex(seasonSelectedIndex) as? Season)?.episodes)!

        self.collectionView.reloadData()
        self.seasonCollection.reloadData()
    }
}

Obs: Im using multiple collectionviews in the same view, and the nib files are named correctly Obs:我在同一视图中使用了多个collectionview,并且nib文件的命名正确

Is there any reason why the iPhone 4S does load the viewDidLoad method after the notification has been posted? 通知发布后,iPhone 4S是否有理由加载viewDidLoad方法?

Managed to solve my problem. 设法解决了我的问题。 The iphone 4s screen size wasn't big enough to trigger the cellForIndex event and the class/observer wasn't initiated until the phone screen scrolled down. iPhone 4s的屏幕尺寸不足以触发cellForIndex事件,并且直到手机屏幕向下滚动时才启动类/观察程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM