2012-04-09 2 views
1

[OK]を、私は本当にこれをカバーするように見えるが、まだ私のために働くものを見つけていないトピックを検索しました。このリストは、すべてのUIWebViewページのロードをここに送信することによって行われます。ある時点で、ユーザーがこのリストをクリアしたいのであれば、クリアしたいと確認してからOKを押してクリアするようなアラートビューを表示するボタンがあります。どうすればいいのか教えてください。あなたは私のclearAllDataがclearAllRecentsアラートボタンですが、その下の私のvoid関数が私のことを見つけることができます。それを行う必要がありますアラートボタンでUITableView内のすべてのデータオブジェクトを削除またはクリアしますか?

#import "RecentViewController.h" 
#import "ViewController.h" 

@interface RecentViewController() 

@end 

@implementation RecentViewController 

@synthesize recent, explorerView; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    recent = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"Recent"] mutableCopy]; 
    [super viewDidLoad]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (IBAction)cancelButtonTapped 
{ 
    [self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; 
    [self.presentingViewController dismissModalViewControllerAnimated:true]; 
} 

- (IBAction)clearAllRecents 
{ 
    UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"clear all recents?" 
         message:@"press ok to clear" 
         delegate: self 
         cancelButtonTitle:@"cancel" 
         otherButtonTitles:@"ok", nil]; 
    [alert show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1) { 
     //clear all recent objects?????????????????????????????????????????????? 
    } 
} 

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

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

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

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

    cell.textLabel.text = [recent objectAtIndex:(recent.count - indexPath.row - 1)]; 
    cell.textLabel.textColor = [UIColor whiteColor];; 
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    explorerView.urlField.text = [recent objectAtIndex:(recent.count - indexPath.row - 1)]; 
    [explorerView textFieldShouldReturn:explorerView.urlField]; 
    [self.presentingViewController dismissModalViewControllerAnimated:true]; 
    explorerView = nil; 
    recent = nil; 
    tableView = nil; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [recent removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]; 
     [[NSUserDefaults standardUserDefaults] setObject:recent forKey:@"Recent"]; 
    } 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

答えて

3
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  { 

if (buttonIndex == 1) { 
    [recent removeAllObjects]; 
    [[NSUserDefaults standardUserDefaults] setObject:recent forKey:@"Recent"]; 
    [yourTableView reloadData]; 
}} 

+0

私はこれを試しましたが、私が得る問題は[yourTableView reloadData]です。あなたのテーブルビューの粗さは私にとっては適切ではありません。私はUITableViewを試して、reloadDataは知られていません。 tableViewだけでエラーが発生し、私はUITableViewを使用することを提案します。 – user1264599

+0

私はこれがUIViewControllerのuitableviewだと言及しませんでした。そこで、未知のメソッドを@property(nonatomic、retain)を追加して修正しました。IBOutlet UITableView * myTableView;私の.hに接続し、それをIBで接続します。だから、テーブルはクリアされますが、テーブルビューに戻ると直ちにすべてのデータが再び入力されます。私は何が欠けていますか? [:mutableCopy] "最近" @ [[NSUserDefaults standardUserDefaults] arrayForKey]; - – user1264599

+0

あなたの問題は、 'さ(無効) { 最近の=のviewDidLoad [super viewDidLoad]; } ' たびにビューをロード、' recent'は、ユーザーのデフォルトの値が割り当てられています。値を完全に消去したい場合は、 'arrayForKey:@" Recent "をリセットする必要があります。それ以外の場合は、値をロードする別の方法を見つける必要があります。おそらく、ユーザーにrecentsをリロードするオプションを与えます。 (編集された答えを参照) – AMayes

関連する問題