Swift UiAlertController after another at runtime - swift

I found some solutions solving the problem to show more than one UIAlertController but they aren't working for me. While starting the app it could be possible that more than one alert needs to be shown. So when I call the showAlert() function I do not know if there will be more alerts. With rootViewController.presentViewController(..) it's only possible to show one and the others will be ignored. So what I need to do is to check if one alert is presented otherwise queue the alert and after closing the first alert check if there are more alerts to show. My idea was to solve this with NSOperation and NSOperationQueue, saying that just one Operation can be executed in time. Something like this:
var alertQueue : NSOperationQueue = NSOperationQueue()
func showAlert(alertController: UIAlertController) {
alertQueue.maxConcurrentOperationCount = 1
alertQueue.addOperation(NSOperationAlert(alertController: alertController))
}
The NSOperation:
class NSOperationAlert: NSOperation {
let aController : UIAlertController
init(alertController : UIAlertController) {
aController = alertController
}
override func start() {
AppRouter.sharedInstance.window.rootViewController?.presentViewController(aController, animated: true, completion: nil)
}
}
But obviously something is missing. I don't know exactly how to solve that the next queued alert is shown after the other alert isn't shown anymore. In other words how do I know that the currently presented Alert is "finished" without adding a handler to each button and tell this my alertQueue to execute the next alert if there is one.
Thanks a lot !

Related

Simple data transfer within viewcontroller without transition

So i am new to app development and i am trying to set up a very simple delegation/protocol pattern. I have been searching and trying different tutorials but can't seem to find anything that works and am getting in such a muddle. Please can somebody help. I will break i down so that its really clear as to what i need -
I have two view controllers, 'DetailedVC' and 'SelectionsVC'.
DetailedVC has a variable called -
var sendingData = (choice: "", choiceValue:0.0)
and
UIbutton buttonSelectTapped
SelectionsVC has a variable called -
var recievedData = (choice: "", choiceValue:0.0)
And all i want to do is send the data from the variable 'sendingData' in DetailedVC when the button (buttonSelectTapped) is tapped to the SelectionsVC and store it in the variable 'recievedData'. I do not want the VC to transition from one to the other or anything to be sent back, only to send the data to the other VC.
Then when the user views that controller 'SelectionsVC' at whatever stage, the data will be called in the viewDidLoad when loading that controller.
Use NSUserDefault to pass data between viewcontroller if you do not want the VC to transition from one to the other or anything to be sent back, only to send the data to the other VC.
DetailedVC Code
func viewDidLoad() {
NSUserDefaults.standardUserDefaults().removeObjectForKey("selectedData")
}
func didTapButtonSelectTapped() {
NSUserDefaults.standardUserDefaults().setDouble(sendingData , forKey: "selectedData")
}
SelectionsVC code
func viewDidLoad() {
if(NSUserDefaults.standardUserDefaults().doubleForKey("selectedData")) {
recievedData = NSUserDefaults.standardUserDefaults().doubleForKey("selectedData")
}
}
But as your question title describe their is no use of protocol/delegate in above code.
Passing Data on transition from viewcontroller :
DetailedVC Code
func didTapButtonSelectTapped() {
let vc = SelectionsVC()
vc.recievedData = sendingData
self.presentViewController(vc, animated: true, completion: nil)
}

Displaying Activity indicator with OCR and using threads

I am trying to display an activity indicator whilst some text recognition happens. If i just start and stop[ the indicator around the recognition code it never shows. The issue i have is that if use:
activityIndicator.startAnimating()
DispatchQueue.main.async( execute: {
self.performTextRecognition()
})
activityIndicator.stopAnimating()
self.performSegue(withIdentifier: "segueToMyBills", sender: self)
The indicator never shows as it performs the segue and the table view in the next view controller shows no information because the text recognition hasn't completed. I've never touched on threads until now so a little insight on what to do would be much appreciated. Thanks!
Well your problem is that your OCR happens on the main thread. That blocks the thread thus never having time to draw the activity indicator. Try to modify your code into this:
activityIndicator.startAnimating()
DispatchQueue.global(qos: .background).async { [weak weaKSelf = self] in
// Use a weak reference to self here to make sure no retain cycle is created
weakSelf?.performTextRecognition()
DispatchQueue.main.async {
// Make sure weakSelf is still around
guard let weakSelf = weakSelf else { return }
weakSelf.activityIndicator.stopAnimating()
weakSelf.performSegue(withIdentifier: "segueToMyBills", sender: weakSelf)
}
}
Try to add a completion handler to your self.performTextRecognition() function in this way
function performTextRecognition(completion: ((Error?) -> Void)? = .none) {
//you can replace Error with Any type or leave it nil
//do your logic here
completion?(error)
}
and then call the function like this :
performTextRecognition(completion: { error in
activityIndicator.stopAnimating()
self.performSegue(withIdentifier: "segueToMyBills", sender: self)
})

Capture self was never used

I don't post the entire code because it's really too large. Anyway, it's a user registration built this way.
There is a modal whose child is a pageViewController:
the first page is for Login;
the second page is for Registration;
From Login you can reach Registration and, once registered, pageViewController should automatically transition to the first page, so that the user can login with his new credentials.
The process of user registration is managed by a button and Alamofire: once the button is clicked, the values the user inserted in the textfields are validated, then I start a post request, sending data to the server and receiving JSON data back in a while.
It's anything very simple (sorry for speaking too much), but in the end, after I receive the JSON, something strange happens here:
let j = JSON as! NSDictionary
let userStatus = j.object(forKey: "status")!
if((userStatus as! Int) == 0){
//
print("user registerd")
let alert = UIAlertController(title: "Registration ok", message: "Registration OK", preferredStyle:UIAlertControllerStyle.alert)
let okAlert = UIAlertAction(title:"OK", style: .default, handler: {action in v.scrollToPreviousViewController() })
alert.addAction(okAlert)
self.present(alert, animated: true, completion: nil)
What is supposed to do this code? Once the user clicks the alert button, the pageviewcontroller should return with an animation to the login screen.
What happens? It returns back there but without animation.
This led me to think I should avoid "polluting" the global thread reserved to the GUI. And, in fact, I tried to put all the code inside a:
DispatchQueue.main.async { [unowned self] in
without any success.
Then, I tried another thing:
let okAlert = UIAlertAction(title:"OK", style: .default, handler:
{action in DispatchQueue.main.async { [unowned self] in v.scrollToPreviousViewController() }})
I don't perfectly understand why, but this practically works and eliminates the issue with the animation.
But that unowned self there generates a warning: "Capture self was never used". What am I doing wrong?
If you didn't use self inside the async block then you will get this warning obviously. Please try with _ instead of self.
to see what is swift capture list for, check this example ...
var i = 25
var cl = {print(i)}
var clc = {[i] in print(i)}
i = 35
cl() // 35
clc() // 25
the warning message "Capture self was never used" is therefore self-explanatory. you don't need to put self in your capture list, because you don't use it there

How to use an NSAlert with storyboards

I'm teaching myself Swift (currently using Xcode 7.3) and I'm working with storyboards for the first time. I'm writing an OS X-based app and I want to display an alert when the user attempts to load data when data already exists. I've read the following thread, Add completion handler to presentViewControllerAsSheet but I'm having trouble wrapping my head around closures/completion handlers. I understand them "in theory" but not yet well enough to write one.
In the thread above, a Struct is being returned. I just need to return an Int or Bool to indicate whether the user wants to overwrite the data or not.
You don't need to create a second view controller. Just configure and display an NSAlert object:
#IBAction func loadData(sender : AnyObject) {
let dataAlreadyExists = true // assume this is always true
if dataAlreadyExists {
let alert = NSAlert()
alert.messageText = "Do you want to reload data?"
alert.addButtonWithTitle("Reload")
alert.addButtonWithTitle("Do not reload")
alert.beginSheetModalForWindow(self.view.window!) { response in
if response == NSAlertFirstButtonReturn {
// reload data
}
}
}
}

performSegue after completion handler

ANSWER BELOW
Im facing a little issue that you may help me with.
the app Im working on allows you to request for content based on your location.
the first ViewController is somewhat a form that grab your location / a specified location + some other information to target specific answers.
I need to perform a segue to pass the "question" variables to my second ViewController where I load "answers" with a query based on the question details.
What is causing me trouble is that, whenever the question is geolocalized, I can't retrieve the information using prepareForSegue because it doesn't wait for the geoPoint to be made (completed).The second controller display my latitude and longitude as nil.
I see that I can call the "prepareForSegue" method using "perfomSegueWithIdentifier", and retrieve the information in my second view controller but it perform the segue twice... How can I trigger the segue only when Im ready but using the prepareForSegue data parameter I need to preserve?
Is there a way to pass variable from one controller to another using performSegue?
Any help would be awesome.
Also, while I don't think the code is relevant for my question, here is the code I use.
geoPointing method
#IBAction func doPostQuestion(sender: UIButton) {
var thereQ:PFObject = PFObject(className: "tquestion")
if(somewhereLabel.text == "my location"){
println("Location is geolocalized")
PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint: PFGeoPoint!, error: NSError!) -> Void in
if error == nil {
self.geoLati = geoPoint.latitude as Double
self.geoLong = geoPoint.longitude as Double
self.performSegueWithIdentifier("goto_results", sender:self) // call prepareForSegue when ready but implies to have a segue done on click... (performed twiced)
}
}
}
self.navigationController?.popToRootViewControllerAnimated(true)
}
prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "goto_results"){
// Get Label
let theDestination = (segue.destinationViewController as displayAnswersViewController)
theDestination.lat = self.geoLati
theDestination.lng = self.geoLong
}
}
ANSWER SOLUTION:
As suggested, to solve this problem you just need to create your segue from your viewController1 to your viewController2 and not from a button. This way you can trigger prepareForSegue programatically using the "performSegue" method that will call prepareForSegue anyway.
To solve this problem you just need to create your segue from your viewController1 to your viewController2 and not from a button. This way you can trigger prepareForSegue programatically using the "performSegue" method that will call prepareForSegue anyway.