2014-10-27 50 views
0

我想知道如何設置進度條等於xcode中的最大用戶輸入。ProgressView/ProgressBar 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; 
} 

這只是我的代碼snipet,該inputamount =進度條就是我想做的事,但我真的不知道該怎麼做。我希望最大的輸入量等於progressviewbar。所以我將不得不比較所有添加到數組中的#。有任何想法嗎?謝謝!

回答

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