Xcode 6.1.1 - I have written small apps with a button. I have written IBAction method showMessage() in ViewController.swift. When I was trying to connect button and view controller. The View Controller icon in storyboard didn't show the "Sent Event"---> showMessage method.
#IBAction func showMessage() {
let alertController = UIAlertController(title: "Welcome to My First App", message: "Hello World", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
Your method is fine. When I tried it, it worked and showMessage did show up under Sent Events. As #Daniel Nagy alluded to in the comments, you should make sure you set your class correctly in the Storyboard.
Click on the ViewController icon at the top of your View Controller.
In the Identity Inspector on the right, set the Class to the Class of your View Controller.
Thanks. I have noticed that the Module field is empty. It is not filled with my project name such as Current - HelloWorld
Related
I need the authentication via TouchID and FaceID and various "else" requests in my app. I managed to integrate it, so that after pressing the "button" to proceed, you move on to another VIEW.
The problem is that if the "cancel" item is pressed, however, the button that is connected to the next VIEW continues to work. I would like if the user presses "cancel" it will be shown on the home page. The Button is connected via Main.Storyboard to the second VIEW Controller created.
Below is the part of the code I wrote:
#IBAction func touchID(_ sender: Any){
let context:LAContext = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
{
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Autorization Required", reply: { (wasSuccessful, error) in
if wasSuccessful {
print("Correct")
//let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewControllerID") as! SecondViewController
//self.present(vc, animated: true, completion: nil)
}
else
{
print("Incorrect")
}
})
}else{
print("TouchID/Facec ID not configured")
}
}
}
I am a beginner.
Try to do this:
Your "button" to proceed must call to your TouchID function. When you print "correct", you must create the navigation by code. If you print "Incorrect", dont create the navigation.
If you print "TouchID/Face ID not configured", you should show an alertview and maybe open the app settings configuration to enable/disable touch/faceId.
In my app i have a storyboard which has initial view controller as X and then i have a navigation controller which has couple of view controllers attached with it through segue but there is no connection between ViewController x and this navigation controller.
Now based on a button tap in View controller x i want to present this navigation controller.
I am doing this :
func showDashBoard()
{
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let next = storyBoard.instantiateViewControllerWithIdentifier("navigation") as! EBNavigationController
self.presentViewController(next, animated: true, completion: nil)
}
So the navigation view is getting presented with the root view controller but its not showing me animation and also i am getting a warning "Attempt to present on whose view is not in the window hierarchy!
"
Can some one please help me in figuring out what is the best way to do this.
Thanks,
The animation problem can be fixed using next.modalPresentationStyle and next.modalTransitionStyle. Lookup the enums for the possible values and give it the one you want.
You usually get the "Attempt to present on whose view is not in the window hierarchy!" errors when you are calling this function before the view is fully loaded. For instance if you call it in the viewDidLoad method. You should call it only after viewDidAppear is called.
That's the button action:
#IBAction func readyPressed(sender: NSButton) {
self.dismissController(Hardness)
println(1)
self.performSegueWithIdentifier("manual", sender: self)
}
When I press the button, println works, segue works, but NSViewController "Hardness" doesn't close
Try this line to close the window: self.view.window?.close()
You must add Action from button to First Responder - dismissController:
you can try
NSViewController().dismiss(vc).
the viewcontroller to be dismissed should be passed as argument and the caller could be any NSViewController object.
Please try using
self.dismissViewControllerAnimated(true, completion: {});
or use
navigationController.popViewControllerAnimated(true)
as presented here
how to dismiss a view controller in swift
I've tried to build on a Cocoa app which uses storyboard and Swift in Xcode 6. However, when I tried to alter the title of window from within NSViewController, the following code doesn't work.
self.title = "changed label"
When I wrote the above code in viewDidLoad() function, the resultant app's title still remains window.
Also, the following code causes an error, since View Controller doesn't have such property as window.
self.window.title = "changed label"
So how can I change the title of window programmatically in Cocoa app which is built on storyboard?
There are 2 problems with your code:
viewDidLoad is called before the view is added to the window
NSViewController does not have a window property
To fix the first one, you could override viewDidAppear(). This method is called after the view has fully transitioned onto the screen. At that point it is already added to a window.
To get a reference to the window title, you can access a view controller's window via its view: self.view.window.title
Just add the following to your view controller subclass, and the window title should change:
override func viewDidAppear() {
super.viewDidAppear()
self.view.window?.title = "changed label"
}
This worked for me, currentDict is NSDictionary passed from previous viewController
var currentDict:NSDictionary?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if let myString:String = currentDict?["title"] as? String {
self.title = myString
}
}
I am new to IOS developing and recently started in Xcode 4.5. I saw for every viewController that i could set some identity variables including the storyboard ID. What is this, and how can I use it?
I started searching on stackoverflow and couldn't find any explanation for it.
I assumed it's not just some stupid label that I can set to remember my controller right? What does it do?
The storyboard ID is a String field that you can use to create a new ViewController based on that storyboard ViewController. An example use would be from any ViewController:
//Maybe make a button that when clicked calls this method
- (IBAction)buttonPressed:(id)sender
{
MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
This will create a MyCustomViewController based on the storyboard ViewController you named "MyViewController" and present it above your current View Controller
And if you are in your app delegate you could use
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
Edit: Swift
#IBAction func buttonPressed(sender: AnyObject) {
let vc = storyboard?.instantiateViewControllerWithIdentifier("MyViewController") as MyCustomViewController
presentViewController(vc, animated: true, completion: nil)
}
Edit for Swift >= 3:
#IBAction func buttonPressed(sender: Any) {
let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController
present(vc, animated: true, completion: nil)
}
and
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
To add to Eric's answer and update it for Xcode 8 and Swift 3:
A storyboard ID does exactly what the name implies: it identifies. Just that it identifies a view controller in a storyboard file. It is how the storyboard knows which view controller is which.
Now, don't be confused by the name. A storyboard ID doesn't identify a 'storyboard'. A storyboard, according to Apple's documentation, 'represents the view controllers for all or part of your app’s user interface.' So, when you have something like the picture below, you have a storyboard called Main.storyboard which has two view controllers, each of which could be given a storyboard ID (their ID in the storyboard).
You can use a view controller's storyboard ID to instantiate and return that view controller. You can then go ahead to manipulate and present it however you want. To use Eric's example, say you want to present a view controller with identifier 'MyViewController' when a button is pressed, you would do it this way:
#IBAction func buttonPressed(sender: Any) {
// Here is where we create an instance of our view controller. instantiateViewController(withIdentifier:) will create an instance of the view controller every time it is called. That means you could create another instance when another button is pressed, for example.
let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController
present(vc, animated: true, completion: nil)
}
Please take note of changes in syntax.