IOS development-Table store

Source: Internet
Author: User

IOS development-Table store
According to the above concept, the page layout is about a 3*3 9-square grid. It is generally used to select a number exercise, and there is no special way to get it done. In the Demo implementation, the above interface is mainly composed of numbers 1, therefore, you need a method to generate a random array. The method is as follows. The general rule for generating a random and non-repeating array is to first create an array. Then, you need to disrupt the array, use a random number to randomly generate an index:-(NSArray *) getDataArray {// The array NSMutableArray * arr = [NSMutableArray array]; for (NSInteger I = 1; I <10; I ++) {[arr addObject: @ (I)] ;}nsinteger count = [arr count]; // generates a random array for (NSInteger I = 0; I <count; I ++) {NSInteger index = arc4random () % (count-I) + I; [arr exchangeObjectAtIndex: index withObjectAtIndex: I];} return arr;} assign the value in the array to the button on the page:-(void) initButtonTitle {// variable array of the instantiated result set self. resultArr = [NSMutableArray array]; NSArray * arr = [self getDataArray]; for (UIButton * button in self. visionButtons) {NSString * result = [arr [button. tag-1] stringValue]; [button setTitle: result forState: UIControlStateNormal]; // you need to reset the background and status of the button when you start again. [button setBackgroundColo R: [UIColor yellowColor]; [button setEnabled: YES];} [_ timerLabel setText: @ "00:00"]; self. timer = [NSTimer scheduledTimerWithTimeInterval: 0.1 invocation: nil repeats: YES];} call the corresponding program at viewDidLoad startup: 1 [self initButtonTitle]; this step is basically completed by the program, followed by timing. After the completion of the statistics, take a look at the specific effect, and then see the code may be better: the basic function is to set the background color of the button. Can you click, time, reset, or pop-up button to display the previously clicked results? Step by step analysis: defines the timing and storage results of member variables: @ interface ViewController () @ property NSMutableArray * re SultArr; @ property NSTimer * timer; @ property NSDate * beginDate; @ end: set the timer to self. timer = [NSTimer scheduledTimerWithTimeInterval: 0.1 invocation: nil repeats: YES]; set the background color and invisibility of the button: [button setBackgroundColor: [UIColor yellowColor]; [button setEnabled: YES]; statistical time: NSInteger allTime = [_ timer. fireDate timeIntervalSinceDate: _ beginDate]; NSString * timeFormat = [NSString stringWithFormat: @ "% 02ld: % 02ld", allTime/1000, allT Ime % 1000]; [_ timerLabel setText: timeFormat]; judge the result. If all the results are in ascending order, they are successful; otherwise, they fail: // determine whether an array is in ascending order-(BOOL) getArrayAsc :( NSArray *) originalArr {for (NSInteger I = 0; I <[originalArr count]-1; I ++) {if (originalArr [I]> originalArr [I + 1]) {return NO;} return YES;} click events for all buttons:-(IBAction) getResult :( id) sender {UIButton * button = (UIButton *) sender; // set the background color [button setBackgroundColor: [UIColor greenColor]; // button After one click, click [button setEnabled: NO]; NSInteger value = [[[button titleLabel] text] integerValue]; [self. resultArr addObject: [NSNumber numberWithInteger: value]; // click the first button and set the start time if ([self. resultArr count] = 1) {// game start time _ beginDate = [NSDate date]; // game start [_ timer fire];} NSInteger allTime = [_ timer. fireDate timeIntervalSinceDate: _ beginDate]; NSString * timeFormat = [NSString stringWithFormat: @ "% 02ld: % 02ld", al LTime/1000, allTime % 1000]; [_ timerLabel setText: timeFormat]; // call if ([self. resultArr count] = 9) {// destroy the timer [_ timer invalidate]; // The NSString * tip in the pop-up box; if ([self getArrayAsc: self. resultArr]) {tip = @ "congratulations, pass the competition";} else {tip = @ "sorry, the competition failed ";} // concatenate the clicked result with a comma. NSString * resultStr = [NSString stringWithFormat: @ "% @", [self. resultArr componentsJoinedByString: @ ","]; UIAlertView * alterView = [[UIAlertView al Loc] initWithTitle: tip message: resultStr delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alterView show] ;}} ViewController. code in m: // ViewController. m // TableVision /// Created by keso on 15/1/18. // Copyright (c) 2015 keso. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ property NSMutableArray * resultArr; @ property NSTimer * timer; @ property NSDate * be GinDate; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self initButtonTitle]; // self. timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: nil userInfo: nil repeats: YES];}-(void) triggerTime :( NSTimer *) sender {}-(void) initButtonTitle {// variable array of the instantiated result set self. resultArr = [NSMutableAr Ray array]; NSArray * arr = [self getDataArray]; for (UIButton * button in self. visionButtons) {NSString * result = [arr [button. tag-1] stringValue]; [button setTitle: result forState: UIControlStateNormal]; // you need to reset the background and status of the button when you start again. [button setBackgroundColor: [UIColor yellowColor]; [button setEnabled: YES];} [_ timerLabel setText: @ "00:00"]; self. timer = [NSTimer scheduledTimerWithTimeInterval: 0.1 invocation: nil Repeats: YES];}-(IBAction) oneMore :( id) sender {[self initButtonTitle];}-(NSArray *) getDataArray {// The array NSMutableArray * arr = [NSMutableArray array]; for (NSInteger I = 1; I <10; I ++) {[arr addObject: @ (I)];} NSInteger count = [arr count]; // generates a random array for (NSInteger I = 0; I <count; I ++) {NSInteger index = arc4random () % (count-I) + I; [arr exchangeObjectAtIndex: index withObjectAtIndex: I];} return arr ;}-( IBAction) getResult :( id) sender {UIButton * button = (UIButton *) sender; // set the background color [button setBackgroundColor: [UIColor greenColor]; // click the button once and NO longer click [button setEnabled: NO]; NSInteger value = [[[button titleLabel] text] integerValue]; [self. resultArr addObject: [NSNumber numberWithInteger: value]; // click the first button and set the start time if ([self. resultArr count] = 1) {// game start time _ beginDate = [NSDate date]; // game start [_ timer fire];} NS Integer allTime = [_ timer. fireDate timeIntervalSinceDate: _ beginDate]; NSString * timeFormat = [NSString stringWithFormat: @ "% 02ld: % 02ld", allTime/1000, allTime % 1000]; [_ timerLabel setText: timeFormat]; // call if ([self. resultArr count] = 9) {// destroy the timer [_ timer invalidate]; // The NSString * tip in the pop-up box; if ([self getArrayAsc: self. resultArr]) {tip = @ "congratulations, pass the competition";} else {tip = @ "sorry, the competition failed" ;}// concatenate the clicked results with commas (,) to NSString * ResultStr = [NSString stringWithFormat: @ "% @", [self. resultArr failed: @ ","]; UIAlertView * alterView = [[UIAlertView alloc] initWithTitle: tip message: resultStr delegate: nil cancelButtonTitle: @ "OK" failed: nil]; [alterView show] ;}}- (void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} // determine whether an array is in ascending order-(BOOL) getArr AyAsc :( NSArray *) originalArr {for (NSInteger I = 0; I <[originalArr count]-1; I ++) {if (originalArr [I]> originalArr [I + 1]) {return NO;} return YES ;}@ end www.bkjia.com: what I get from my hard work, if you have reprinted the post, you will make a special statement that the blog will not be surprised, but you want to share your personal learning knowledge with others and improve your life and learning. The ownership of the blog belongs to you and the blog Park, if you have reposted a post, please provide a link to the blog and the author's name in a prominent position. Otherwise, I will put it into law.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.