AP102 2016/5/11 上午
Navigation Bar
Bar Button Itam 做ㄧ個新增的方法
呼叫被刪除的指令
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
這時候只會出現動作而已
當結合下方的指令才會產生結果
switch (editingStyle) {
case UITableViewCellEditingStyleDelete:
break;
case UITableViewCellEditingStyleInsert:
break;
case UITableViewCellEditingStyleNone:
break;
default:
break;
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (editingStyle) {
case UITableViewCellEditingStyleDelete:
[self.history removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case UITableViewCellEditingStyleInsert:
break;
case UITableViewCellEditingStyleNone:
break;
default:
break;
}
OBJ-C在switch裡面只能判斷數字
使用C語言的ENUM來判斷
UITableViewCellEditingStyleDelete:
UITableViewCellEditingStyleInsert:
UITableViewCellEditingStyleNone:
開一個類別GuessViewController在Controller資料夾
在.m裡面實作方法
#import "GuessViewController.h"
@interface GuessViewController ()
@end
@implementation GuessViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)randomNumber{
NSArray *arrey=@[@0,@1,@2,@3,@4,@5,@6,@7,@8,@9];
NSInteger index = arc4random() %10;
NSset
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
然後加入這個方法在viewDidload方法下方
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)randomNumber{
//產生隨機數字
NSArray *array=@[@0,@1,@2,@3,@4,@5,@6,@7,@8,@9];
NSMutableSet * picked = [[NSMutableSet alloc] init];
while(picked.count !=4){
NSInteger index = arc4random() %10;
NSNumber*unit = array[index];
[picked addObject:unit];
}
NSArray * answer = [picked allObjects];
NSLog(@"%@",answer);
}
//取得文字框的字
-(void)getUserGuess{
NSString * userGuess=self.playTextfield.text;
NSInteger guessNumber= userGuess.integerValue;
}
這裡有兩種方式可以寫抓取四位數字
方法1
NSInteger* thousand = guessNumber/1000;
guessNumber %=1000;
NSInteger *hundred = guessNumber/100;
guessNumber %=100;
NSInteger *ten = guessNumber/10;
guessNumber%=10;
NSInteger *unit =guessNumber %10;
方法2
NSNumber *thousand=[NSNumber numberWithInteger:(guessNumber/1000)] ;
guessNumber %=1000;
NSNumber *hundred =[NSNumber numberWithInteger:(guessNumber/100)] ;
guessNumber %=100;
NSNumber *ten = [NSNumber numberWithInteger:(guessNumber/10)] ;
guessNumber %=10;
要記得去main storyboard 設定GuessViewController類別
在開始寫
@property (weak, nonatomic) IBOutlet UITextField *playTextField;
- (IBAction)getResult:(UIButton *)sender {
[self randomNumber];
[self getUserGuess];
}
-(void)randomNumber{
//產生隨機數字
NSArray *array=@[@0,@1,@2,@3,@4,@5,@6,@7,@8,@9];
NSMutableSet * picked = [[NSMutableSet alloc] init];
while(picked.count !=4){
NSInteger index = arc4random() %10;
NSNumber*unit = array[index];
[picked addObject:unit];
}
NSArray * answer = [picked allObjects];
NSLog(@"%@",answer);
}
//取得文字框的字
-(void)getUserGuess{
NSString * userGuess=self.playTextField.text;
NSInteger guessNumber= userGuess.integerValue;
NSNumber *thousand=[NSNumber numberWithInteger:(guessNumber/1000)] ;
guessNumber %=1000;
NSNumber *hundred =[NSNumber numberWithInteger:(guessNumber/100)] ;
guessNumber %=100;
NSNumber *ten = [NSNumber numberWithInteger:(guessNumber/10)] ;
guessNumber %=10;
NSNumber *unit =[NSNumber numberWithInteger:(guessNumber%10)] ;
NSArray * guessArray = @[thousand,hundred,ten,unit];
NSLog(@"%@",guessArray);
}
改成全域變數以後
讓button可以取用輸入框的變數和隨機產生的變數
然後利用NSMutableSet進行比對
#import "GuessViewController.h"
@interface GuessViewController ()
@property (weak, nonatomic) IBOutlet UITextField *playTextField;
@property (strong, nonatomic)NSArray*guessArray;
@property (strong, nonatomic)NSArray*answer;
@end
@implementation GuessViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self randomNumber];
// Do any additional setup after loading the view.
}
- (IBAction)getResult:(UIButton *)sender {
// [self randomNumber];
[self getUserGuess];
NSMutableSet * answerSet = [NSMutableSet setWithArray:self.answer];
NSMutableSet * userSet = [NSMutableSet setWithArray:self.guessArray];
[answerSet intersectSet:userSet];
NSLog(@"%@",answerSet);
}
-(void)randomNumber{
//產生隨機數字
NSArray *array=@[@0,@1,@2,@3,@4,@5,@6,@7,@8,@9];
NSMutableSet * picked = [[NSMutableSet alloc] init];
while(picked.count !=4){
NSInteger index = arc4random() %10;
NSNumber*unit = array[index];
[picked addObject:unit];
}
self.answer = [picked allObjects];
NSLog(@"%@",self.answer);
}
//取得文字框的字
-(void)getUserGuess{
NSString * userTyped=self.playTextField.text;
NSInteger guessNumber= userTyped.integerValue;
NSNumber *thousand=[NSNumber numberWithInteger:(guessNumber/1000)] ;
guessNumber %=1000;
NSNumber *hundred =[NSNumber numberWithInteger:(guessNumber/100)] ;
guessNumber %=100;
NSNumber *ten = [NSNumber numberWithInteger:(guessNumber/10)] ;
guessNumber %=10;
NSNumber *unit =[NSNumber numberWithInteger:(guessNumber%10)] ;
self.guessArray = @[thousand,hundred,ten,unit];
NSLog(@"%@",self.guessArray);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
GUI
Navigation Bar
Bar Button Itam 做ㄧ個新增的方法
Syntax
NSSet 無序(沒有順序的)
NSMutableSet
NSMutableSet * answerSet = [NSMutableSet setWithArray:self.answer];
[answerSet intersectSet:userSet];
NSNumber *unit =[NSNumber numberWithInteger:(guessNumber%10)] ;
Comments
Post a Comment