Clicking the Button will move to the next controller? - swift

How to Move to the Next Controller with a single click of a Button?
There is no data transfer, only one click of button and it will proceed to the next controller.
I created a controller named "ViewIntro" which is the initial controller. It's basically a Welcome Screen and there is a button at the bottom.
Once the button is clicked, it will proceed to the viewController which is the original controller whenever we start an xcode project I forgot to make the viewController as my welcome screen
Anyways, here is my current code of my ViewIntro:
`import Foundation
import UIKit
class ViewIntro : UIViewController{
#IBOutlet weak var btnEnter: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}`
I also had connected the ViewIntro to ViewController through identifier as shown in the attached image.

Right click your button and drag it to your controller. Just like creating an IBOutlet but you should select Action mode. Afret creating an IBAction for your button, just add this code into it:
self.performSegue(withIdentifier: "showMain", sender: nil)

Related

How do I enable miniaturize button of new/additional NSViewController in swift macOS App

step1. I new a project: macOS App, swift and storyboard
step2. The default NSViewController named InitialViewController. I add a button named open.
step3. I add a NSViewController named NewViewController.
step4. I linked the button to the NewViewController with Modal kind
step5. I run the App and click the button, and then I found that the miniaturize button of NewViewController is disable.
How do I enable the miniaturize button of NewViewController? I have tried to add the code shown below but it's not working.
class NewViewController: NSViewController {
override func viewDidAppear() {
self.view.window?.standardWindowButton(.miniaturizeButton)?.isEnabled = true
}
}

UITapGesture not recognised on the simulator when tapping a UIImageView

I am trying to display a new image based on a user tapping the image view. I have added a UITapGestureRecognizer on top of the UIImageView which has been defined a "displayPhoto" and connected it as an outlet to the view controller.
#IBOutlet weak var displayPhoto: UIImageView!
#IBAction func changeImage(_ sender: UITapGestureRecognizer) {
displayPhoto.image = UIImage(named: "myimage")
}
The sent action is listed as follows:
sent actions
When I run the app and click on the image, nothing happens. I've even tried to make the first line of the IBAction function a fatal error but nothing happens. What am I missing?
Thanks in advance for any help.
The full view and connection
Tap Gesture Recognizer Window
Firstly you try; clean your project
command+option+shift+K
The first case cannot be empty
You should add a picture.
override func viewDidLoad() {
super.viewDidLoad()
displayPhoto.image = UIImage(named: "firsCase")
}

How to reference a View from within a Window Controller?

I'm having a Window Controller with a toolbar. I also have a View Controller containing some views. How do I reference a view from the View Controller within my Window Controller? I'm still learning macOS development and I'm missing the bigger picture how code is structured and classes are meant to interact.
My concrete problem right now is this: Using XCode 9.4.1 I have a window with a toolbar and a button in it. That's how my WindowsController.swift looks like:
import Cocoa
class WindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
window?.titleVisibility = .hidden
}
#IBAction func startExport(_ sender: NSButton) {
print("Start Export")
}
}
In the ViewControllerScene there's a WKWebView that's loading a web page. When the button in the toolbar is pressed, I want to call that Web Views takeSnapshot method. So I need a reference in WindowsController.swift to that Web View, but control-dragging the Web View from the storyboard to WindowsController.swift in the assistant editor doesn't let me create that outlet.
This:
let vc = contentViewController as? ViewController
will take you to your view controller.

Adding views with IBAction to a NSStackView crashes application

I want to use the NSStackView to stack views above each other, I also want them to de able to expand so I can't use the NSCollectionView if i understood it correctly.
So, in storyboard, I've created a NSStackView(embedded in scroll view) in the main view controller and a view controller that I want to fill it with:
The button will fill the stack view with ten views:
#IBOutlet weak var stackView: NSStackView!
#IBAction func redrawStackView(_ sender: Any) {
for i in 0..<10 {
let stackViewItemVC = storyboard?.instantiateController(withIdentifier: "StackViewItemVC") as! StackViewItemViewController
stackViewItemVC.id = i
stackView.addArrangedSubview(stackViewItemVC.view)
}
}
And the ViewController on the right simply looks like this:
class StackViewItemViewController: NSViewController {
var id: Int = -1
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
#IBAction func buttonPressed(_ sender: Any) {
debugPrint("StackViewItemViewController" + id.description + "pressed")
}
Running this small application works fine, every time I press the button ten more stack view items appears. But, when I have the audacity to press one of the buttons to the right the application crashes:
Where am I going wrong?
I have tried to work around the IBAction to verify that this what breaks, and the application will not crash if I subclass the button and make a "buttonDelegate" protocol with a function being called from mouseUp.
I guess the problem is that the viewController objects, which you create in the loop, are released immediately.
Even though the view is attached to the stackView, it's viewController is destroyed.
You can fix this issue by keeping a reference to each viewController.
You can do this by creating a new variable
var itemViewControllers = [StackViewItemViewController]()
and then add each newly created viewController to it:
itemViewController.append(stackViewItemVC)

Enable user interaction of a button SWIFT

OK, so lets assume the button created below is greyed out and user interaction disabled.
How do I enable the button from anywhere else.
I know how to disable the button from inside the button function, sender.enabled = false, but I don't want to disable it from there.
I want to re-enable the user interaction from outside the button function.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func loadButton(sender: UIButton) {
//i wan to enable interaction of this button via override func and NSUserDefaults
}
}
In iOS and OS X development you have so-called outlets that are variables pointing to UI elements. You can read more about them here.
To declare an outlet, you prefix your variable with #IBOutlet like so:
#IBOutlet weak var button: UIButton!
and then you need to connect your outlet to the button in your xib file. That can be done in several ways, but if you have declared a variable like the above, you can:
go to your storyboard
in the document outline hold down the control button
while holding down the control button, you drag from your files owner or ViewController to the UIButton you'd like to connect to
select the outlet you'd like to connect to (button in this case)
As shown here
Once that is in place, you are free to use enable your button (and much more) in your code, for instance:
func viewDidLoad() {
super.viewDidLoad()
button.enabled = false
}
You can read more about IBOutlets here
And here is another way to connect outlets (this is how I prefer to connect outlets actually, you define and connect in one go)
Hope that helps