2017-07-27 71 views
0

我目前有一個UIPageViewController,並且我需要它在完成塊完成時更改文本,同時停留在同一頁上。我試圖做這樣說:有沒有辦法在同一頁面上更改UIPageViewController中的標籤文本?

func addChainedAnimationView (titleLabel: String, descriptionLabel: String){ 
// 
(completion: {(finished) in 
    DispatchQueue.main.sync { 
          self.label1?.text = titleLabel 

         } 
     }) 
} 

,但我得到一個錯誤

EXC_ BAD_指令

這是視圖控制器是如何構建的,它縮短了,所以你可以忽略一些的參數:

public class WelcomePageView : UIView 
{ 
    public var animationStrings : [String]? 
    public var titleStrings: [String]? 
    public var descriptionStrings: [String]? 

    var index: Int = 0 
} 

    var label1 : UILabel? 
    var label2 : UILabel? 

    func buildViewControllerWithImage(titleString : String, titleString2 : String?, descriptionString: String, descriptionString2: String?, backgroundImage: String, jsonFile : String, jsonFile2 : String?, index : Int, loopCount: Int, userType: User, isLottieChained: Bool) -> UIViewController 
    { 

    let vc : UIViewController = UIViewController(nibName: "WelcomePageView", bundle: Bundle(for: type(of: self))) 
     (vc.view as? WelcomePageView)!.index = index 
     vc.view.frame = self.view.frame 
     var paragraphStyle = NSMutableParagraphStyle() 

    var label1String = titleString 
     if(!(label1String.contains("\n"))) { 
      label1String = "\n"+label1String 
     } 
     paragraphStyle = NSMutableParagraphStyle() 
     paragraphStyle.lineSpacing = 0 
     paragraphStyle.alignment = .center 
     paragraphStyle.minimumLineHeight = 24 
     label1 = (vc.view.viewWithTag(2) as? UILabel)! 
     label1?.font = UIFont.CustomBold(size: 20) 

     var attrString = NSMutableAttributedString(string: label1String) 
     attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) 
     (vc.view.viewWithTag(2) as? UILabel)!.attributedText = attrString 
     return vc 
    } 

我這樣得到它們:

func getViewControllerWithImage(_ index: Int, userType: User, country: Country) -> UIViewController 

    { 
      vc = self.buildViewControllerWithImage(titleString:"FIRST_SCREEN_TITLE".localized, titleString2: nil, descriptionString: "FIRST_SCREEN_DESCRIPTION".localized, descriptionString2: nil, backgroundImage: "fractal_blue", jsonFile: "Welcome_Step_01_V01", jsonFile2: nil, index: index, loopCount: loopCount, userType: userType, isLottieChained: false) 

return vc 
    } 

他們在這裏呼籲:

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 

    var index : Int = (viewController.view as? WelcomePageView)!.index // currentIndex = index 

//currentIndex = index 
//(viewController.view as? WelcomePageView)!.index 

print ("XXXXX after \(index) XXXXXXX ") 
//index = self.currentIndex 
index += 1 

if (index == maxPages) 
{ 
    index = 0 
} 
return self.getViewControllerWithImage(index, userType: User.newUser, country: 

checkCountry()) 
} 

回答

0

我相信你的問題可能是在提醒sync,而不是async上DispatchQueue.main。

您已經在主隊列中,所以您不應該調用同步,因爲它會導致主隊列本身等待的死鎖。

相關問題