2 Replies - 576 Views - Last Post: 30 July 2017 - 05:18 AM

#1 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 217
  • Joined: 02-February 15

Segue to a different view, if condition on current view is met?

Posted 30 July 2017 - 04:42 AM

Hello all,

I have a view in my iOS app at the moment which does a lookup to an external database, when a button is pressed. It takes the input in a textField and returns a match or no match from the DB table.

If there is a match I want the app to load my next view, if there is no match I want it to remain on the current view.

Below is the code behind the button, and I'm using the storyboard GUI to drag from the button to the next view and creating a segue. Anybody know how I could achieve the above ?

@IBAction func buttonSearchPostCode(_ sender: UIButton) {
        if (textFieldPostcode.text != "") {
            let requestURL = URL(string: URL_CHECK_POSTCODE)
            
            var request = URLRequest(url: requestURL!)
            
            request.httpMethod = "POST"
            
            let postcode = (String(describing: textFieldPostcode.text!.prefix(4))).uppercased()
            
            let postParameters = "postcode="+postcode;
            
            request.httpBody = postParameters.data(using: .utf8)
            
            let task = URLSession.shared.dataTask(with: request) { data,response,error in guard let data = data, error == nil else {
                print("error=\(String(describing: error))")
                return
                }
                
                if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
                    print("statusCode should be 200, but is \(httpStatus.statusCode)")
                    print("response = \(String(describing: response))")
                }
                
                let responseString = String(data: data, encoding: .utf8)!
                print("response = \(String(describing: responseString))")
                
                if (responseString.contains("\"error\":false")) {
                    print ("Load menu")
                }
                
            }
            task.resume()
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Segue to a different view, if condition on current view is met?

#2 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 217
  • Joined: 02-February 15

Re: Segue to a different view, if condition on current view is met?

Posted 30 July 2017 - 05:04 AM

So I created a function in the same class as the button:

    func showMenu(sender: UIButton) {
        performSegue(withIdentifier: "ShowMenuValidPostcode", sender: sender)
    }



Then where I was previously printing "Load Menu", I'm now calling the function:

self.showMenu(sender: sender)



but it crashes my app. The error code is below, anybody able to help :P ?

CoreSimulator/Devices/27614D13-D3C0-465B-917A-36227B7BE388/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-07-30 13:01:06.367746+0100 SpiceCentral[15375:4927040] [MC] Reading from private effective user settings.
response = {"error":false,"message":"Valid postcode area"}
2017-07-30 13:01:10.174434+0100 SpiceCentral[15375:4927475] *** Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3692.2/Keyboard/UIKeyboardTaskQueue.m:433
2017-07-30 13:01:10.181393+0100 SpiceCentral[15375:4927475] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104f33aeb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001010c4f41 objc_exception_throw + 48
2 CoreFoundation 0x0000000104f38bb2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x0000000100b76d06 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 UIKit 0x0000000102afbba6 -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 380
5 UIKit 0x00000001020c6b66 -[UIKeyboardImpl setDelegate:force:] + 1596
6 UIKit 0x000000010259e55c -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1192
7 UIKit 0x00000001025a798c -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 498
8 UIKit 0x0000000101f8dc0a -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1119
9 UIKit 0x0000000101f8f911 -[UIViewController _presentViewController:withAnimationController:completion:] + 4621
10 UIKit 0x0000000101f923f6 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 134
11 UIKit 0x0000000101f92896 -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 532
12 UIKit 0x0000000101f92332 -[UIViewController presentViewController:animated:completion:] + 181
13 UIKit 0x0000000101f97794 -[UIViewController _showViewController:withAction:sender:] + 274
14 UIKit 0x0000000102522ead __66-[UIStoryboardShowSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 134
15 UIKit 0x00000001026d65ea -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 279
16 UIKit 0x00000001026d64a2 -[UIStoryboardSegueTemplate _perform:] + 82
17 UIKit 0x0000000101f7ee70 -[UIViewController performSegueWithIdentifier:sender:] + 99
18 SpiceCentral 0x00000001007b86d0 _T012SpiceCentral28PostcodeSearchViewControllerC8showMenuySo8UIButtonC6sender_tF + 128
19 SpiceCentral 0x00000001007b8111 _T012SpiceCentral28PostcodeSearchViewControllerC06buttonD8PostCodeySo8UIButtonCFy10Foundation4DataVSg_So11URLResponseCSgs5Error_pSgtcfU_ + 2753
20 SpiceCentral 0x00000001007b847c _T012SpiceCentral28PostcodeSearchViewControllerC06buttonD8PostCodeySo8UIButtonCFy10Foundation4DataVSg_So11URLResponseCSgs5Error_pSgtcfU_TA + 124
21 SpiceCentral 0x00000001007b8597 _T010Foundation4DataVSgSo11URLResponseCSgs5Error_pSgIxxxx_So6NSDataCSgAGSo7NSErrorCSgIyByyy_TR + 263
22 CFNetwork 0x00000001053a2178 __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 19
23 CFNetwork 0x00000001053a19dd __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 147
24 Foundation 0x0000000100ae9057 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
25 Foundation 0x0000000100ae8eba -[NSBlockOperation main] + 68
26 Foundation 0x0000000100ae7329 -[__NSOperationInternal _start:] + 793
27 libdispatch.dylib 0x0000000105f98354 _dispatch_client_callout + 8
28 libdispatch.dylib 0x0000000105f9d945 _dispatch_block_invoke_direct + 593
29 libdispatch.dylib 0x0000000105f98354 _dispatch_client_callout + 8
30 libdispatch.dylib 0x0000000105f9d945 _dispatch_block_invoke_direct + 593
31 libdispatch.dylib 0x0000000105f9d6d4 dispatch_block_perform + 109
32 Foundation 0x0000000100ae3328 __NSOQSchedule_f + 342
33 libdispatch.dylib 0x0000000105f98354 _dispatch_client_callout + 8
34 libdispatch.dylib 0x0000000105f9e718 _dispatch_continuation_pop + 967
35 libdispatch.dylib 0x0000000105f9cad6 _dispatch_async_redirect_invoke + 780
36 libdispatch.dylib 0x0000000105fa3ff7 _dispatch_root_queue_drain + 772
37 libdispatch.dylib 0x0000000105fa3caf _dispatch_worker_thread4 + 44
38 libsystem_pthread.dylib 0x00000001063ff5a2 _pthread_wqthread + 1299
39 libsystem_pthread.dylib 0x00000001063ff07d start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Was This Post Helpful? 0
  • +
  • -

#3 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 217
  • Joined: 02-February 15

Re: Segue to a different view, if condition on current view is met?

Posted 30 July 2017 - 05:18 AM

Found a solution, I will paste below incase anybody ever runs into this problem...

change the function I created in my first reply to the following:

    func showMenu(sender: UIButton) {
        DispatchQueue.main.async { [weak self] in
            self?.performSegue(withIdentifier: "ShowMenuValidPostcode", sender: sender)
        }
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1