Basic segmented control not working

我是研究僧i 提交于 2019-12-11 14:44:53

问题


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:


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.



来源:https://stackoverflow.com/questions/45257746/basic-segmented-control-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!