我目前正在编写我的第一个iOS应用程序并遇到了一个我无法自己解决的问题或以前的问题。我目前有一个有两个视图的应用程序,它由一个ViewController.m文件控制。我想在第一个视图中单击百分比按钮时更改第二个视图中显示的标签中的文本。
我一直在尝试通过为它们创建Outlets作为属性来修改标签。由于某种原因,代码正在执行,而不是显示在屏幕上。我想知道这个问题是否与我目前设置的关系有关,或者我调用的标签不正确我不确定。如果有人能帮助我,我将不胜感激。 (特别是在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
答案 0 :(得分:0)
听起来您可能没有将该属性连接到您的UILabel。
在@property
声明的行号旁边,有一个应该填充的小灰圈。如果不是,您需要控制 - 将UILabel拖动到Interface Builder中的ViewController,然后选择resultTitle作为插座。