Basic segmented control not working

2019-08-28 20:00发布

I am trying to implement a segmented control into a view controller but every time I try to tap the controller on the simulator, the app crashes. But I really don't know what is from with my code. For added context: Trying to change four labels with four segments.

//
//  AboutViewController.swift
//  Yiives
//
//  Created by Patrick van der Nat on 7/22/17.
//  Copyright © 2017 Origen. All rights reserved.
//

import UIKit

class AboutViewController: UIViewController {
    @IBOutlet weak var segmentedControl: UISegmentedControl!
    @IBOutlet weak var textLabel: UILabel!

    @IBAction func indexChanged(_ sender: Any) {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            textLabel.text = "First Segment Selected";
        case 1:
            textLabel.text = "Second Segment Selected";
        case 2:
            textLabel.text = "Third Segment Selected";
        case 3:
            textLabel.text = "Fourth Segment Selected";
        default:
            break
        }
    }
}

And here is the error that is given:

2017-07-22 20:02:31.059244+0200 Yiives[369:50130] -[Yiives.AboutViewController segmentControl:]: unrecognized selector sent to instance 0x100b37b40
2017-07-22 20:02:31.060083+0200 Yiives[369:50130] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Yiives.AboutViewController segmentControl:]: unrecognized selector sent to instance 0x100b37b40'
*** First throw call stack:
(0x183a1afe0 0x18247c538 0x183a21ef4 0x183a1ef54 0x18391ad4c 0x189b81010 0x189b80f90 0x189b6b504 0x189c9a764 0x189d522e0 0x189b80390 0x189b7b728 0x189b4c33c 0x18a346014 0x18a340770 0x18a340b9c 0x1839c942c 0x1839c8d9c 0x1839c69a8 0x1838f6da4 0x185360074 0x189bb1058 0x1000a8544 0x18290559c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

1条回答
Ridiculous、
2楼-- · 2019-08-28 20:25

You've connected an action method named segmentControl: but in your code the action method is named indexChanged.

Update your connection from the segmented control outlet to the action method.

查看更多
登录 后发表回答