Objective-C(iOS):无法修改标签';s文本

Objective-C(iOS):无法修改标签';s文本,ios,objective-c,Ios,Objective C,我目前正在编写我有史以来的第一个iOS应用程序,遇到了一个我自己或以前的问题都无法解决的问题。我目前有一个应用程序,它有两个视图,并由一个ViewController.m文件控制。当单击第一个视图中的百分比按钮时,我想更改第二个视图中显示的标签中的文本 我一直在尝试通过创建插座作为属性来修改标签。由于某些原因,正在执行的代码没有显示在屏幕上。我想知道这个问题是否与我目前建立的关系有关,或者可能是我给标签打错了电话,我不确定。如果有人能帮助我,我将不胜感激。(特别是在printResults方法中

我目前正在编写我有史以来的第一个iOS应用程序,遇到了一个我自己或以前的问题都无法解决的问题。我目前有一个应用程序,它有两个视图,并由一个ViewController.m文件控制。当单击第一个视图中的百分比按钮时,我想更改第二个视图中显示的标签中的文本

我一直在尝试通过创建插座作为属性来修改标签。由于某些原因,正在执行的代码没有显示在屏幕上。我想知道这个问题是否与我目前建立的关系有关,或者可能是我给标签打错了电话,我不确定。如果有人能帮助我,我将不胜感激。(特别是在printResults方法中。在一条评论中,我添加了一个链接,如果有人感兴趣,可以查看工作流的外观。)

问题的浓缩版本:

// Label I would like to change the text of (in a second view in the same controller)
@property (strong, nonatomic) IBOutlet UILabel *resultTitle;

- (void) printResults:(int) percent :(int) weightOnBar :(Weights*) weightObject{
   // How I am trying to modify the text in a method within the viewcontroller.m file
   [self.fortyFivePlateLabel setText: [NSString stringWithFormat:@"Test"]];
}
ViewController.m

#import "ViewController.h"
#import "Weights.h"

@interface ViewController ()

// Objects represent the text fields
@property (strong, nonatomic) IBOutlet UITextField *weightField;
@property (strong, nonatomic) IBOutlet UITextField *repsField;

// Objectsrepresent the percentage buttons below the fields
@property (strong, nonatomic) IBOutlet id nintyFiveResult;
@property (strong, nonatomic) IBOutlet id nintyResult;
@property (strong, nonatomic) IBOutlet id eightyFiveResult;
@property (strong, nonatomic) IBOutlet id eightyResult;
@property (strong, nonatomic) IBOutlet id seventyFiveResult;
@property (strong, nonatomic) IBOutlet id seventyResult;
@property (strong, nonatomic) IBOutlet id sixtyFiveResult;
@property (strong, nonatomic) IBOutlet id sixtyResult;

// Weight object used to perform the on bar calculation
@property (strong, nonatomic) Weights *weightObj;

// Labels represent the values on the second view
@property (strong, nonatomic) IBOutlet UILabel *resultTitle;
@property (strong, nonatomic) IBOutlet UILabel *fortyFivePlateLabel;
@property (strong, nonatomic) IBOutlet UILabel *thirtyFivePlateLabel;
@property (strong, nonatomic) IBOutlet UILabel *twentyFivePlateLabel;
@property (strong, nonatomic) IBOutlet UILabel *tenPlateLabel;
@property (strong, nonatomic) IBOutlet UILabel *fivePlateLabel;
@property (strong, nonatomic) IBOutlet UILabel *twoFivePlateLabel;

@end

@implementation ViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.weightField.delegate = self;
    self.repsField.delegate = self;
    self.weightObj = [Weights getInstance];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

/* weightModified: This method is called when the weight field is modified.
   If the reps or weight value entered is equal to zero all of the percentage
   results will be changed to zero. If valid values are passed in both fields
   the percentages of weight will be calculated.
 */
- (IBAction)weightModified:(UITextField *)sender {

    NSLog(@"Weight Input Value: %@", sender.text);
    NSLog(@"Reps Input Value: %@", self.repsField.text);

    if([sender.text isEqualToString:@""] == false && [self.repsField.text isEqualToString:@""] == false){

        NSLog(@"Weight Modified in if statement.");

        // Gather reps and weight value
        int reps = 0;
        int weight = 0;
        weight = [sender.text intValue];
        reps = [self.repsField.text intValue];

        NSLog(@"Weight: %d", weight);
        NSLog(@"Reps: %d", reps);

        int nintyFiveInt = [self oneRepMax :0.95 :reps :weight];
        int nintyInt = [self oneRepMax :0.9 :reps :weight];
        int eightyFiveInt = [self oneRepMax :0.85 :reps :weight];
        int eightyInt = [self oneRepMax :0.8 :reps :weight];
        int seventyFiveInt = [self oneRepMax :0.75 :reps :weight];
        int seventyInt = [self oneRepMax :0.7 :reps :weight];
        int sixtyFiveInt = [self oneRepMax :0.65 :reps :weight];
        int sixtyInt = [self oneRepMax :0.6 :reps :weight];

        NSString *nintyFiveWeight = [NSString stringWithFormat:@"95%%: %d lbs", nintyFiveInt];
        NSString *nintyWeight = [NSString stringWithFormat:@"95%%: %d lbs", nintyInt];
        NSString *eightyFiveWeight = [NSString stringWithFormat:@"95%%: %d lbs", eightyFiveInt];
        NSString *eightyWeight = [NSString stringWithFormat:@"95%%: %d lbs", eightyInt];
        NSString *seventyFiveWeight = [NSString stringWithFormat:@"95%%: %d lbs", seventyFiveInt];
        NSString *seventyWeight = [NSString stringWithFormat:@"95%%: %d lbs", seventyInt];
        NSString *sixtyFiveWeight = [NSString stringWithFormat:@"95%%: %d lbs", sixtyFiveInt];
        NSString *sixtyWeight = [NSString stringWithFormat:@"95%%: %d lbs", sixtyInt];

        [self.nintyFiveResult setTitle:nintyFiveWeight forState:UIControlStateNormal];
        [self.nintyResult setTitle:nintyWeight forState:UIControlStateNormal];
        [self.eightyFiveResult setTitle:eightyFiveWeight forState:UIControlStateNormal];
        [self.eightyResult setTitle:eightyWeight forState:UIControlStateNormal];
        [self.seventyFiveResult setTitle:seventyFiveWeight forState:UIControlStateNormal];
        [self.seventyResult setTitle:seventyWeight forState:UIControlStateNormal];
        [self.sixtyFiveResult setTitle:sixtyFiveWeight forState:UIControlStateNormal];
        [self.sixtyResult setTitle:sixtyWeight forState:UIControlStateNormal];

    }

    else{
        [self.nintyFiveResult setTitle:@"95%%: 0 lbs" forState:UIControlStateNormal];
        [self.nintyResult setTitle:@"90%%: 0 lbs" forState:UIControlStateNormal];
        [self.eightyFiveResult setTitle:@"85%%: 0 lbs" forState:UIControlStateNormal];
        [self.eightyResult setTitle:@"80%%: 0 lbs" forState:UIControlStateNormal];
        [self.seventyFiveResult setTitle:@"75%%: 0 lbs" forState:UIControlStateNormal];
        [self.seventyResult setTitle:@"70%%: 0 lbs" forState:UIControlStateNormal];
        [self.sixtyFiveResult setTitle:@"65%%: 0 lbs" forState:UIControlStateNormal];
        [self.sixtyResult setTitle:@"60%%: 0 lbs" forState:UIControlStateNormal];
    }

}

/* repsModified: This method is called when the weight field is modified.
 If the reps or weight value entered is equal to zero all of the percentage
 results will be changed to zero. If valid values are passed in both fields
 the percentages of weight will be calculated.
 */
- (IBAction)repsModified:(UITextField *)sender {

    if([sender.text isEqualToString:@""] == false && [self.repsField.text isEqualToString:@""] == false){

        NSLog(@"Reps Modified in if statement.");

        // Gather reps and weight value
        int reps = 0;
        int weight = 0;
        weight = [self.weightField.text intValue];
        reps = [sender.text intValue];

        NSLog(@"Weight: %d", weight);
        NSLog(@"Reps: %d", reps);

        int nintyFiveInt = [self oneRepMax :0.95 :reps :weight];
        int nintyInt = [self oneRepMax :0.9 :reps :weight];
        int eightyFiveInt = [self oneRepMax :0.85 :reps :weight];
        int eightyInt = [self oneRepMax :0.8 :reps :weight];
        int seventyFiveInt = [self oneRepMax :0.75 :reps :weight];
        int seventyInt = [self oneRepMax :0.7 :reps :weight];
        int sixtyFiveInt = [self oneRepMax :0.65 :reps :weight];
        int sixtyInt = [self oneRepMax :0.6 :reps :weight];

        NSString *nintyFiveWeight = [NSString stringWithFormat:@"95%%: %d lbs", nintyFiveInt];
        NSString *nintyWeight = [NSString stringWithFormat:@"95%%: %d lbs", nintyInt];
        NSString *eightyFiveWeight = [NSString stringWithFormat:@"95%%: %d lbs", eightyFiveInt];
        NSString *eightyWeight = [NSString stringWithFormat:@"95%%: %d lbs", eightyInt];
        NSString *seventyFiveWeight = [NSString stringWithFormat:@"95%%: %d lbs", seventyFiveInt];
        NSString *seventyWeight = [NSString stringWithFormat:@"95%%: %d lbs", seventyInt];
        NSString *sixtyFiveWeight = [NSString stringWithFormat:@"95%%: %d lbs", sixtyFiveInt];
        NSString *sixtyWeight = [NSString stringWithFormat:@"95%%: %d lbs", sixtyInt];

        [self.nintyFiveResult setTitle:nintyFiveWeight forState:UIControlStateNormal];
        [self.nintyResult setTitle:nintyWeight forState:UIControlStateNormal];
        [self.eightyFiveResult setTitle:eightyFiveWeight forState:UIControlStateNormal];
        [self.eightyResult setTitle:eightyWeight forState:UIControlStateNormal];
        [self.seventyFiveResult setTitle:seventyFiveWeight forState:UIControlStateNormal];
        [self.seventyResult setTitle:seventyWeight forState:UIControlStateNormal];
        [self.sixtyFiveResult setTitle:sixtyFiveWeight forState:UIControlStateNormal];
        [self.sixtyResult setTitle:sixtyWeight forState:UIControlStateNormal];
    }

    else{
        [self.nintyFiveResult setTitle:@"95%%: 0 lbs" forState:UIControlStateNormal];
        [self.nintyResult setTitle:@"90%%: 0 lbs" forState:UIControlStateNormal];
        [self.eightyFiveResult setTitle:@"85%%: 0 lbs" forState:UIControlStateNormal];
        [self.eightyResult setTitle:@"80%%: 0 lbs" forState:UIControlStateNormal];
        [self.seventyFiveResult setTitle:@"75%%: 0lbs" forState:UIControlStateNormal];
        [self.seventyResult setTitle:@"70%%: 0 lbs" forState:UIControlStateNormal];
        [self.sixtyFiveResult setTitle:@"65%%: 0 lbs" forState:UIControlStateNormal];
        [self.sixtyResult setTitle:@"60%%: 0 lbs" forState:UIControlStateNormal];
    }
}

/* oneRepMax: This method is used to compute the one rep according to the values passed
   percent: represents the percentages displayed on the screen
   numReps: represents the number of reps in the reps field
   weightToLift: represents the number in the wieght field
   The method returns a float of the weight to be displayed on screen
 */
- (float)oneRepMax:(float) percent :(int) numReps :(int) weightToLift{

    float r = (float) numReps;
    float w = (float) weightToLift;
    float max = w*(1+(r/30));

    return percent*max;
}

/* printResults: This method is used to print the results of the weight on bar in thesecond UI View
   percent: represents the percentage of one rep max selected to put on the bar
   weightOnBar: represents the total weight to be computed into plates
   weightObject: used to compute the plate values
 */
- (void) printResults:(int) percent :(int) weightOnBar :(Weights*) weightObject{
    [self.resultTitle setText:[NSString stringWithFormat:@"%d%%: %d", percent, weightOnBar]];
    [self.weightObj setTotalWeight:weightOnBar];

    [self.fortyFivePlateLabel setText: [NSString stringWithFormat:@"Test"]];
    [self.thirtyFivePlateLabel setText: [NSString stringWithFormat:@"X %d", [self.weightObj getThirtyFives]]];
    [self.twentyFivePlateLabel setText: [NSString stringWithFormat:@"X %d", [self.weightObj getTwentyFives]]];
    [self.tenPlateLabel setText: [NSString stringWithFormat:@"X %d", [self.weightObj getTens]]];
    [self.fivePlateLabel setText: [NSString stringWithFormat:@"X %d", [self.weightObj getFives]]];
    [self.twoFivePlateLabel setText: [NSString stringWithFormat:@"X %d", [self.weightObj getTwoPointFives]]];
}

// Percentage Buttons

- (IBAction)clickNintyFive:(id)sender {
    int weight = [self.weightField.text intValue];
    int reps = [self.repsField.text intValue];
    int nintyFiveInt = [self oneRepMax :0.95 :reps :weight];
    [self printResults: 95: nintyFiveInt: _weightObj];
}

- (IBAction)clickNinty:(id)sender {
    int weight = [self.weightField.text intValue];
    int reps = [self.repsField.text intValue];
    int nintyFiveInt = [self oneRepMax :0.9 :reps :weight];
    [self printResults: 90: nintyFiveInt: _weightObj];
}

- (IBAction)clickEightyFive:(id)sender {
    int weight = [self.weightField.text intValue];
    int reps = [self.repsField.text intValue];
    int nintyFiveInt = [self oneRepMax :0.85 :reps :weight];
    [self printResults: 85: nintyFiveInt: _weightObj];
}

- (IBAction)clickEighty:(id)sender {
    int weight = [self.weightField.text intValue];
    int reps = [self.repsField.text intValue];
    int nintyFiveInt = [self oneRepMax :0.8 :reps :weight];
    [self printResults: 80: nintyFiveInt: _weightObj];
}

- (IBAction)clickSeventyFive:(id)sender {
    int weight = [self.weightField.text intValue];
    int reps = [self.repsField.text intValue];
    int nintyFiveInt = [self oneRepMax :0.75 :reps :weight];
    [self printResults: 75: nintyFiveInt: _weightObj];
}

- (IBAction)clickSeventy:(id)sender {
    int weight = [self.weightField.text intValue];
    int reps = [self.repsField.text intValue];
    int nintyFiveInt = [self oneRepMax :0.7 :reps :weight];
    [self printResults: 70: nintyFiveInt: _weightObj];
}

- (IBAction)clickSixtyFive:(id)sender {
    int weight = [self.weightField.text intValue];
    int reps = [self.repsField.text intValue];
    int nintyFiveInt = [self oneRepMax :0.65 :reps :weight];
    [self printResults: 65: nintyFiveInt: _weightObj];
}

- (IBAction)clickSixty:(id)sender {
    int weight = [self.weightField.text intValue];
    int reps = [self.repsField.text intValue];
    int nintyFiveInt = [self oneRepMax :0.6 :reps :weight];
    [self printResults: 60: nintyFiveInt: _weightObj];
}

@end

听起来您可能没有将属性连接到UILabel


@属性
声明的行号旁边,有一个小灰色圆圈,应该填充。如果不是,则需要控制将UILabel拖动到Interface Builder中的ViewController,然后选择Resultitle作为输出。

工作流图片:Woah。你能把代码简化成更简洁的东西吗?仅显示一个标签,而不是7。添加了一个较小的部分以直接解决问题。您的
UILabel
是否将文本设置为甚至可见,而不是位于其他子视图的下方?我不确定是否意外创建了子视图。(我不知道尝试创建一个视图的第一件事)唯一让我认为这可能是一个原因的是,导致我浏览视图的按钮使用模式分段。但我可以单击第二个视图上的后退按钮返回到原始视图。它已连接。这是一张显示视图中元素连接的图像。如果尝试更改UILabel的另一个属性,例如
[self.fortyFivePlateLabel setHidden:TRUE]?如果要将要更改的标签指定给属性
resultitle
,为什么要更改
self.fortyFivePlateLabel
的文本,而不是
self.resultitle
?我只是尝试将标签设置为隐藏,但它也不起作用。我正在更改这两个标签,因为我希望在导航到视图时更改这两个标签的文本。