ProgressView / ProgressBar Xcode

时间:2014-10-27 01:48:12

标签: xcode progress-bar

我想知道如何将进度条设置为等于xcode中的最大用户输入。

@property (weak, nonatomic) IBOutlet UIProgressView *progressBar;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _inputAmount.keyboardType = UIKeyboardTypeDecimalPad;
    self.amount = [NSMutableArray new];
    [self.amount addObject:@"Total Amount of Push-Ups:"];
    [self.myList setDataSource:self];

    _inputAmount = UIProgressViewStyleBar;
}

这只是我的代码的一小部分,inputamount =进度条是我想要做的,但我不确定如何做到这一点。我希望最大输入量也等于进度视图。所以我必须比较添加到数组中的所有#s。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

//
//  ViewController.m
//  Push Up Tracker
//
//  Created by Paul Lesny on 10/26/14.
//  Copyright (c) 2014 Paul Lesny. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextField *inputAmount;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
@property NSMutableArray *amount;
@property (weak, nonatomic) IBOutlet UITableView *myList;
@property (weak, nonatomic) IBOutlet UIProgressView *progressBar;

@end


@implementation ViewController

//@synthesize progressBar, progressValue;

///NSInteger stringToInt(NSString *string) {
//return [string integerValue];
//}



- (void)viewDidLoad
{
    [super viewDidLoad];


    // Do any additional setup after loading the view, typically from a nib.
    _inputAmount.keyboardType = UIKeyboardTypeDecimalPad;
    self.amount = [NSMutableArray new];
    [self.myList setDataSource:self];
    [self readDataFromFile:@"lalala1"];

    if (_amount.count!=0)
    {
    NSMutableDictionary *max=[_amount objectAtIndex:0];
    NSString *m = max[@"max"];
        NSLog (@"%@",m);
    NSInteger num=[m intValue];
    NSLog(@"amount is not empty");
    _progressBar.progress = (float) [m intValue]/50;
    }
    else
    {
        _progressBar.progress=0;
    }
    //[self.amount addObject:@"0"];
    //[self.myList reloadData];
}




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





-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return[self.amount count];
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * cellId = @"pancake";//identifier for the cells
    // get a cell from the cache
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    // if the cell is not cached then
    if(cell ==nil)
    {
        // create a new cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    NSMutableDictionary *newEntry=_amount[indexPath.row];
    [cell.textLabel setText:newEntry[@"pushUpNum"]];
    // put the corresponding element of the array as text.
    // return the cell.
    return cell;

}




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





-(void) readDataFromFile:(NSString*)fileName
{
    NSData *fileData=[NSData dataWithContentsOfURL:[self urlOfEntries:@"lalala1"]];
    if(fileData!=nil)
    {
        _amount=[NSPropertyListSerialization
                       propertyListWithData:fileData options:
                       NSPropertyListMutableContainers format:nil error:nil];
    }
    else
    {
        _amount=[NSMutableArray new];
    }
}





-(NSURL *) urlOfEntries:(NSString*)name
{
    NSURL *docDirectory=[[[NSFileManager defaultManager]
                          URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
    NSURL* fullPath=[docDirectory URLByAppendingPathComponent:name];
    return fullPath;
}




-(void) savePushUpTotal:(NSMutableArray *)myEntries
{
    [myEntries writeToURL:[self urlOfEntries:@"lalala1"]atomically:YES];
}





- (IBAction)sender:(id)addButton
{
    NSMutableDictionary *newRecord = [[NSMutableDictionary alloc]init];
    [newRecord setObject:self.inputAmount.text forKey:@"pushUpNum"];

    if (_amount.count==0)
    {
        [newRecord setObject:@"0" forKey:@"max"];
    }

    [self.amount addObject:newRecord];
    [self savePushUpTotal:_amount];
    [self.myList reloadData];
    [self.inputAmount resignFirstResponder];

    [self savePushUpTotal:_amount];


    NSMutableDictionary *max=_amount[0];
    NSString *m = max[@"max"];
    NSInteger num=[m intValue];

    NSMutableDictionary *newDictionary=_amount[_amount.count-1];

    NSString *blah = newDictionary[@"pushUpNum"];
    NSInteger number=[blah intValue];
    NSLog(@"dadala%@",blah);

    if (_amount.count!=1)
    {
        if (number>num)
        {
            _progressBar.progress= (float)number/50;
             NSString *pushUp = max[@"pushUpNum"];

            NSMutableDictionary *maxPush = [[NSMutableDictionary alloc]init];
            [maxPush setObject:pushUp forKey:@"pushUpNum"];


            [maxPush setObject:blah forKey:@"max"];


            [_amount removeObjectAtIndex:(NSUInteger)0];
            [_amount insertObject:maxPush atIndex:0];
            [self savePushUpTotal:_amount];

        }

    }
    else
    {
        _progressBar.progress = (float)number/50;
    }
}


- (IBAction)clear:(UIButton *)sender
{
    _progressBar.progress=0;
    [_amount removeAllObjects];
    [[NSUserDefaults standardUserDefaults] setObject:_amount forKey:0];
    [_myList reloadData];
}


@end