2013-04-03 79 views
0

這是我在我的.m文件保存方法按鈕

@interface HomeWorkViewController() 



@end 

@implementation HomeWorkViewController 
@synthesize adView; 
@synthesize myTableView, numbers; 


-(void) viewDidLoad 
{ 

    adView.delegate=self; 
    [super viewDidLoad]; 

     self.navigationItem.leftBarButtonItem = self.editButtonItem; 

    // check here if key exists in the defaults or not, if yes the retrieve results in array 
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"] != nil) { 
     self.numbers = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"]]; 

    } 

    //Register for the notification when user go to background or minimize the app, just save the array objects in the defaults 

    [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(appWillGoToBackground:) 
                name:UIApplicationWillResignActiveNotification 
               object:[UIApplication sharedApplication]]; 

    //Add the Add button 
    UIBarButtonItem * addButton = [[UIBarButtonItem alloc] 
            initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target: self action: @selector(insertNewObject)]; 

    self.navigationItem.rightBarButtonItem = addButton; 
} 

-(void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.myTableView setEditing:editing animated:animated]; 

} 
-(void)appWillGoToBackground:(NSNotification *)note { 
    NSLog(@"terminate"); 

    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; 
    [defaults setObject:self.numbers forKey:@"numberArray"]; 
    [defaults synchronize]; 

} 

-(void)insertNewObject{ 
    //Display a UIAlertView 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Enter HomeWork" message: @"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; 


    alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
    [alert show]; 


} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    //Only perform the following actions if the user hits the ok button 
    if (buttonIndex == 1) 
    { 
     NSString * tmpTextField = [alertView textFieldAtIndex:0].text; 



     if(!self. numbers){ 

      self.numbers = [[NSMutableArray alloc]init]; 
     } 



     [self.numbers insertObject:tmpTextField atIndex:0]; 



     NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

     [self.myTableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; 




    } 

} 

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     return 1; 
    } 

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

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     static NSString *cellIdentifier = @"cell"; 

     UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

     if(cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault 
              reuseIdentifier: cellIdentifier]; 
     } 

     cell.textLabel.text = [self.numbers objectAtIndex:indexPath.row]; 

     return cell; 
    } 

    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return YES; 

    } 

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     if (editingStyle == UITableViewCellEditingStyleDelete) 
     { 
      //remove our NSMutableArray 
      [self.numbers removeObjectAtIndex:indexPath.row]; 
      //remove from our tableView 
      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 


    } 


- (void)tableView:(UITableView *)tableView 
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath 
toIndexPath:(NSIndexPath *)toIndexPath 
{ 


} 
-(void)bannerViewDidLoadAd:(ADBannerView *)banner 
{ 
    adView.hidden=FALSE; 
    NSLog(@"Has ad, showing"); 

} 

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error 
{ 
    adView.hidden=TRUE; 
    NSLog(@"Has no ads, hiding"); 

} 

-(void)dealloc 
{ 



    [adView release]; 
    [super dealloc]; 


} 


@end 

代碼,我有一個儲蓄的方法有,但我要救我通過點擊按鈕在表中改變了一切。我怎麼做? 我想提出一個工具欄有一個按鈕,說回去要到主屏幕,並把這個按鈕來保存做表樣東西都刪除,切換秩序和補充。

回答

0

您幾乎在那裏:)

您正在使用numbers數組作爲表的數據模型。 確保在處理表格時始終更新此數組。 (例如,您需要重新排序moveRowAtIndexPath中的數字數組,目前,您在該方法中什麼都不做)

要使用按鈕保存模型,只需在Interface Builder中創建UIButton並將其連接到以下操作:

- (IBAction)saveButtonWasPressed:(id)sender { 
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; 
    [defaults setObject:self.numbers forKey:@"numberArray"]; 
    [defaults synchronize]; 
} 

也許你也想突然從navigationcontroller堆棧tableviewcontroller,如果你也想離開控制器。

+0

我用故事板 – user2167312 2013-04-03 22:21:50

+0

如果你只是交給你的代碼永遠也學不會。這是一個很好的教程,可以編輯表格。 http://behindtechlines.com/2012/06/enabling-configuring-uitableview-edit-mode/ – random 2013-04-04 02:43:42

+0

如果我使用分鏡頭腳本 – user2167312 2013-04-04 23:04:11