【问题标题】:Observer being added after postNotification ONLY on iPhone 4S仅在 iPhone 4S 上 postNotification 后添加观察者
【发布时间】:2016-04-21 18:38:56
【问题描述】:

我有一个功能齐全的代码,可以在 iOS >8.0 上完美运行,适用于 iPhone >5 和所有 iPad,我通过 Observers/Notifications 传递信息和调用功能,但在 iPhone 4S 上它不起作用。

通过调试,我发现在 iPhone 4S 上运行时,通知发布后才会添加观察者。

这发生在设备和模拟器上,分别在 iOS 8 和 9 上。

代码:

**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:我在同一个视图中使用多个collectionviews,并且nib文件命名正确

iPhone 4S 在通知发布后会加载 viewDidLoad 方法吗?

【问题讨论】:

  • 验证您的通知是否发布在违规平台的主线程中。那个块(ContentsClient().getContentById(id!).then { 似乎是一个数据获取回调,你也在里面做各种各样的 UI。
  • 是的,它被发布在主线程上,这就是让它变得更加奇怪的原因。这是一个数据获取回调,但我使用处理这些请求的 PromiseKit pod,并且只有当请求结束时,.then 块才会被执行......

标签: ios iphone swift nsnotificationcenter iphone-4


【解决方案1】:

设法解决了我的问题。 iphone 4s 的屏幕尺寸不足以触发 cellForIndex 事件,并且直到手机屏幕向下滚动才启动类/观察者。

【讨论】:

    猜你喜欢
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多