1

I have the following situation:

I have created a rightBarButtonItem in my viewDidLoad:

self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "New", style: .Plain, target: self, action: "newProject")

My question is, how can I move through tapping this button programmatically to another Viewcontroller, using the segue "show (e.g push)".

I have the beginning like this:

func newProject() {

}

But I don't know further, because I'm a beginner in Swift.

Any help would be really appreciated!

Update:

So, this is the whole area, where the error appears:

class NewProject: UIViewController, UITextFieldDelegate, UITextViewDelegate, CustomPickerDelegate {

    @IBOutlet weak var txtProjectName: UITextField!
    @IBOutlet weak var tvDescription: UITextView!
    @IBOutlet weak var txtStartDate: UITextField!
    @IBOutlet weak var txtEndDate: UITextField!

    var hud: MBProgressHUD?
    var customPicker: CustomPickerView?
    var dictProject: NSDictionary!
    var projectID: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "Create new Project"
        self.hud = MBProgressHUD(view: self.view)
        self.view.addSubview(self.hud!)
        self.projectID = ("\(self.dictProject!["id"]!)")
        self.populateFields()
    }
    func populateFields() {
        if nil != self.dictProject!["name"] {
            self.txtProjectName.text = self.dictProject!["name"] as? String
        }

    }

And the error appears on this line here:

self.projectID = ("\(self.dictProject!["id"]!)")

And this line here:

    if nil != self.dictProject!["name"] {
Walker
  • 39
  • 5

1 Answers1

0

Give your segue an identifier in the storyboard (i.e. "NewProject") and then perform it like this:

func newProject() {
    performSegueWithIdentifier("NewProject", sender: self)
}
Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
  • It gives me a crash and the error looks like this:"fatal error: unexpectedly found nil while unwrapping an Optional value". – Walker Mar 02 '16 at 13:15
  • Could you check out where in your code this error is caused? – Tim Vermeulen Mar 02 '16 at 13:24
  • That's not really helpful... Can't you see at which line of code your app crashes? – Tim Vermeulen Mar 03 '16 at 09:02
  • No, I can't. It opens the assembler code and shows me the breakpoints with the error above. – Walker Mar 03 '16 at 09:05
  • Can you see anything useful in the debug navigator in the left panel once your app crashes? Below Thread 1 you should be able to see what's wrong with your code. – Tim Vermeulen Mar 03 '16 at 09:22
  • Yes, fatal error: unexpectedly found nil while unwrapping an Optional value. – Walker Mar 03 '16 at 09:26
  • No, I'm talking about the left panel. You should see Thread 1 with its current stack right below (with `start` at the bottom, and `main` right above). Do you see what I mean? – Tim Vermeulen Mar 03 '16 at 09:30
  • Try setting up an exception breakpoint (Google it if you don't know what it is). That should make it more clear. – Tim Vermeulen Mar 03 '16 at 13:57
  • Either `dictProject` is `nil`, or it has no `"id"` key. – Tim Vermeulen Mar 06 '16 at 00:46
  • But I have 2 other viewcontroller-files where the dictProject is defined the same way. Why would it crash the third time? I have a tableview, in the class of it, I define dictProject the same way as I have mentioned above. And when you tap its cells it navigates to another viewcontroller, where I redefine the SAME WAY dictProject (the second time). And with the rightBarButtonItem I try the same, and it doesn't work? – Walker Mar 06 '16 at 13:42
  • I don't know, but you can easily set a breakpoint right before it crashes to check whether `dictProject` is `nil` or not. – Tim Vermeulen Mar 06 '16 at 14:28
  • Either way, that doesn't really have anything to do with this question anymore. – Tim Vermeulen Mar 06 '16 at 14:29
  • It worked now, due to the breakpoint, I had an idea. Thank you very much for your help, really appreciated. :) – Walker Mar 06 '16 at 20:21
  • I'm glad I could help. :) – Tim Vermeulen Mar 06 '16 at 23:57