Как убедиться, что 1-й (после 0-го) сегмент будет выбран на xcode? - PullRequest
0 голосов
/ 15 марта 2019

Когда я запускаю свой код на симуляторе, я могу использовать левую и правую кнопку для перебора информации об ученике, однако мой код, кажется, не отображает другую страницу (которая связана с 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

1 Ответ

0 голосов
/ 16 марта 2019

В этой строке:

UISegmentedControl *segControl = (UISegmentedControl*)segControl;

Я не вижу в коде никакого объявления для segControl, было ли у вас это свойство где-то еще? Если нет, возможно, вы захотите сделать это вместо этого:

UISegmentedControl *segControl = (UISegmentedControl *)sender;
...