如何在swift中的视图控制器启动时加载json数据

时间:2018-04-27 09:51:14

标签: ios swift

我想在viewcontroller启动时获取数据。每次视图控制器打开它都应该按照JSON更新数据。我添加了application/json中的所有代码以及我将其放在viewDidLoad()中,但它没有加载数据。任何人都可以帮助我知道每次视图控制器打开时我可以做什么来加载数据。

第二个问题:我已经阅读了很多关于此错误的解决方案:viewWillAppear()但我不明白为什么我会收到此错误。单击thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0)时会显示@iBAction,然后在该按钮中单击我使用委托将数据推回到上一个viewcontroller

popViewcontroller.swift:

Button

当我将状态设置为200时,它会收到该错误。

attendance.swift:

protocol DataSendDelegate {
    func sendgetattid(pop_att_id: Int, pop_in_time: String)
}
var delegate: DataSendDelegate? = nil

if statuss == "200" {
    let send_pop_att_id = att_id
    let send_pop_in_time = in_time
    self.navigationController?.popViewController(animated: true)                                  
    self.delegate?.sendgetattid(pop_att_id: send_pop_att_id, pop_in_time: send_pop_in_time)
}

错误:

`EmployeeAttendance [3891:111312]此应用程序在从主线程访问引擎后从后台线程修改autolayout引擎。这可能导致引擎损坏和奇怪的崩溃。

堆栈:(

class attendanceViewController: UIViewController , DataSendDelegate {
    func sendgetattid(pop_att_id: Int, pop_in_time: String) {
        DispatchQueue.main.async {
            self.inTimeTextField.text = pop_in_time
            self.get_att_id = pop_att_id
            self.in_time_button.isEnabled = false
            self.out_time_button.isEnabled = true
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if let popVC = segue.destination as? PopUpViewController {
          popVC.popEmailID = att_emp_id
          popVC.delegate = self
       }
    }
}

)`

0   Foundation                          0x000000010ddb980a _AssertAutolayoutOnAllowedThreadsOnly + 77

1   Foundation                          0x000000010dbc266a -[NSISEngine withBehaviors:performModifications:] + 28

2   UIKit                               0x000000010fc000e3 -[UIView(Hierarchy) _postMovedFromSuperview:] + 855

3   UIKit                               0x000000010fbfdf16 __UIViewWasRemovedFromSuperview + 169

4   UIKit                               0x000000010fbfda07 -[UIView(Hierarchy) removeFromSuperview] + 521

5   UIKit                               0x00000001107a9c5b __46-[UIInputWindowController invalidateInputView]_block_invoke + 127

6   UIKit                               0x000000010fd7a19d -[UIResponder _preserveResponderOverridesWhilePerforming:] + 125

7   UIKit                               0x00000001107a9bd3 -[UIInputWindowController invalidateInputView] + 111

8   UIKit                               0x00000001107aa59d -[UIInputWindowController changeToInputViewSet:] + 222

9   UIKit                               0x00000001107abcbc __43-[UIInputWindowController setInputViewSet:]_block_invoke_2.1494 + 39

10  UIKit                               0x00000001107a2a62 -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] + 617

11  UIKit                               0x00000001107abc8f __43-[UIInputWindowController setInputViewSet:]_block_invoke.1493 + 97

12  UIKit                               0x000000011079b208 -[UIInputWindowController performOperations:withTemplateNotificationInfo:] + 46

13  UIKit                               0x00000001107ab81b -[UIInputWindowController setInputViewSet:] + 1336

14  UIKit                               0x00000001107a2450 -[UIInputWindowController performOperations:withAnimationStyle:] + 50

15  UIKit                               0x000000011033f118 -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1593

16  UIKit                               0x0000000110336f4b -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 2163

17  UIKit                               0x0000000110340480 -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 498

18  UIKit                               0x000000010fd2d94e -[UINavigationController navigationTransitionView:didStartTransition:] + 910

19  UIKit                               0x000000010fd21475 -[UINavigationController _startCustomTransition:] + 3813

20  UIKit                               0x000000010fd373b4 -[UINavigationController _startDeferredTransitionIfNeeded:] + 686

21  UIKit                               0x000000010fd386d3 -[UINavigationController __viewWillLayoutSubviews] + 150

22  UIKit                               0x000000010ff934e2 -[UILayoutContainerView layoutSubviews] + 231

23  UIKit                               0x000000010fc17a6d -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1439

24  QuartzCore                          0x000000011661561c -[CALayer layoutSublayers] + 159

25  QuartzCore                          0x00000001166197ad _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 401

26  QuartzCore                          0x00000001165a086c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 364

27  QuartzCore                          0x00000001165cd946 _ZN2CA11Transaction6commitEv + 500

28  QuartzCore                          0x00000001165cdfbd _ZN2CA11Transaction14release_threadEPv + 213

29  libsystem_pthread.dylib             0x0000000113c2b27e _pthread_tsd_cleanup + 534

30  libsystem_pthread.dylib             0x0000000113c2afbd _pthread_exit + 79

31  libsystem_pthread.dylib             0x0000000113c292ac _pthread_wqthread + 1613

32  libsystem_pthread.dylib             0x0000000113c28c4d start_wqthread + 13

1 个答案:

答案 0 :(得分:1)

你的代码崩溃的地方不清楚。但是我可以提到的是在弹出视图控制器之前调用self.delegate?.sendgetattid(pop_att_id:send_pop_att_id,pop_in_time:send_pop_in_time)。在发送异步调用中包含以上内容。希望它有所帮助。