如何确保在 xcode 上选择第 1 个(第 0 个之后)段?

How make sure the 1st (after 0th) segment to be selected on xcode?

当我运行我的代码在模拟器上时,我可以使用左右键循环访问学生信息,但是,我的代码似乎没有显示另一个页面(连接到subViewController.m) 当我按下第 1 个(第 0 个之后)段时。我不确定我的代码有什么问题。这是我的 ViewController.m 文件。请帮助并提前感谢您!

//
//  ViewController.m
//  Storyboard
//

#import "ViewController.h"
#import "Student_Info.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
_addStudentButton.hidden = YES;
// Do any additional setup after loading the view, typically from a nib.
_studentInfo = [[NSMutableArray alloc]init];

Student_Info *student1 = [Student_Info new];
student1.name = @"King Richard III";
student1.add = @"Leicester Castle, England";
student1.midtermEx = 72;
student1.finalEx = 45;
student1.homework1 = 9;
student1.homework2 = 10;
student1.homework3 = 00;
student1.image = @"richard.png";
[_studentInfo addObject:student1];

Student_Info *student2 = [Student_Info new];
student2.name = @"Prince Hamlet";
student2.add = @"Elsinore Castle, Denmark";
student2.midtermEx = 100;
student2.finalEx = 0;
student2.homework1 = 10;
student2.homework2 = 10;
student2.homework3 = 10;
student2.image = @"younghamlet.png";
[_studentInfo addObject:student2];

Student_Info *student3 = [Student_Info new];
student3.name = @"King Lear";
student3.add = @"Leicester Castle, England";
student3.midtermEx = 100;
student3.finalEx = 22;
student3.homework1 = 10;
student3.homework2 = 6;
student3.homework3 = 0;
student3.image = @"lear.png";
[_studentInfo addObject:student3];

_subVC = [[subViewController alloc] initWithNibName:@"subViewController" bundle:nil];

_arrayIndex = 0;
}

- (IBAction)leftButtonTapped:(id)sender {
if (_arrayIndex != 0) {
    _arrayIndex--;
    Student_Info *studObj = (Student_Info*)[_studentInfo objectAtIndex:_arrayIndex];
    _studentNameTextField.text = studObj.name;
    _studentAddressTextField.text = studObj.add;
    _studentMidtermTextField.text = [NSString stringWithFormat:@"%f", studObj.midtermEx];
    _studentFinalTextField.text = [NSString stringWithFormat:@"%f", studObj.finalEx];
    _studentHWK1TextField.text = [NSString stringWithFormat:@"%i", studObj.homework1];
    _studentHWK2TextField.text = [NSString stringWithFormat:@"%i", studObj.homework2];
    _studentHWK3TextField.text = [NSString stringWithFormat:@"%i", studObj.homework3];
    _letButton.enabled = YES;
    _rightButton.enabled = YES;
}
else {
    _letButton.enabled = NO;
}
}

- (IBAction)rightButtonTapped:(id)sender {
if (_arrayIndex != [_studentInfo count] - 1) {
    _arrayIndex++;
    Student_Info *studObj = (Student_Info*)[_studentInfo objectAtIndex:_arrayIndex];
    _studentNameTextField.text = studObj.name;
    _studentAddressTextField.text = studObj.add;
    _studentMidtermTextField.text = [NSString stringWithFormat:@"%f", studObj.midtermEx];
    _studentFinalTextField.text = [NSString stringWithFormat:@"%f", studObj.finalEx];
    _studentHWK1TextField.text = [NSString stringWithFormat:@"%i", studObj.homework1];
    _studentHWK2TextField.text = [NSString stringWithFormat:@"%i", studObj.homework2];
    _studentHWK3TextField.text = [NSString stringWithFormat:@"%i", studObj.homework3];
    _rightButton.enabled = YES;
    _letButton.enabled = YES;
}
else {
    _rightButton.enabled = NO;
}
}

-(void) clearAllFields {
_studentNameTextField.text = @"";
_studentAddressTextField.text = @"";
_studentHWK1TextField.text = @"";
_studentHWK2TextField.text = @"";
_studentHWK3TextField.text = @"";
_studentMidtermTextField.text = @"";
_studentFinalTextField.text = @"";
}

- (IBAction)segControlTapped:(id)sender {
UISegmentedControl *segControl = (UISegmentedControl*)segControl;
switch(segControl.selectedSegmentIndex){
    case 0  :
        _addStudentButton.hidden = YES;
        break; /* optional */
    case 1  : {
        _addStudentButton.hidden = YES;
        _subVC.studName = _studentNameTextField.text; //ok
        _subVC.studAddress = _studentAddressTextField.text;
        Student_Info *obj = [_studentInfo objectAtIndex:_arrayIndex];
        _subVC.imgName = obj.image;
        break; /* optional */
    }
    case 2  : {
        [self clearAllFields];
        _letButton.enabled = false;
        _letButton.hidden = YES;
        _rightButton.enabled = false;
        _rightButton.hidden = YES;
        break; /* optional */
    }

    default : /* Optional */
        //statement(s);
        break;
}
if (segControl.selectedSegmentIndex == 1) {
    [self presentViewController:_subVC animated:true completion:nil];
}
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
Student_Info *studObj = (Student_Info*)[_studentInfo objectAtIndex:_arrayIndex];
if (textField == _studentNameTextField) {
    studObj.name = textField.text;
}
if (textField == _studentAddressTextField) {
    studObj.add = textField.text;
}
if (textField == _studentMidtermTextField) {
    studObj.midtermEx = textField.text.floatValue;
}
if (textField == _studentFinalTextField) {
    studObj.finalEx = textField.text.floatValue;
}
if (textField == _studentHWK1TextField) {
    studObj.homework1 = (int)textField.text;
}
if (textField == _studentHWK2TextField) {
    studObj.homework2 = (int)textField.text;
}
if (textField == _studentHWK3TextField) {
    studObj.homework3 = (int)textField.text;
}
}

- (IBAction)addStudentPressed:(id)sender {
Student_Info *studObj =[Student_Info new];
if (sender == _studentNameTextField) {
    studObj.name = _studentNameTextField.text;
}
if (sender == _studentAddressTextField) {
    studObj.add = _studentAddressTextField.text;
}
if (sender == _studentMidtermTextField) {
    studObj.midtermEx = _studentMidtermTextField.text.floatValue;
}
if (sender == _studentFinalTextField) {
    studObj.finalEx = _studentFinalTextField.text.floatValue;
}
if (sender == _studentHWK1TextField) {
    studObj.homework1 = (int)_studentHWK1TextField.text;
}
if (sender == _studentHWK2TextField) {
    studObj.homework2 = (int)_studentHWK2TextField.text;
}
if (sender == _studentHWK3TextField) {
    studObj.homework3 = (int)_studentHWK3TextField.text;
}
[_studentInfo addObject:studObj];
}
@end

这一行:

UISegmentedControl *segControl = (UISegmentedControl*)segControl;

我在代码中没有看到 segControl 的任何声明,您在其他地方有这个 属性 吗?如果没有,您可能想要这样做:

UISegmentedControl *segControl = (UISegmentedControl *)sender;