2012-04-03 10 views
0

removeObjectを私の方法の1つに呼び出す必要がありますが、これを正しく行う方法を理解できません。私はObjective-Cの新機能であり、基本を学んでいます。私は写真ギャラリーのように振る舞い、UIImageViewsを表示するアプリを持っています。私はユーザーが自分のギャラリーから写真を削除するオプションを実装しています。これを達成するために、私は各画像の上に目に見えないボタンを配置することに決めました。ユーザーが「編集」ボタンを押すと、各画像上の隠し削除ボタンがアクティブになります(わかりやすくするため、各隠しボタンに同じIBOutletを使用しています)。ユーザーが画像上のボタンをタップすると、実際に削除するかどうかを確認する警告ビューが表示されます。彼らは[はい]をクリックした場合、deleteAlertViewが場に出た:iOS:簡易NSMutableArrayコール

- (void)deleteAlertView:(UIAlertView *)deleteButtonPressed 
     didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (buttonIndex != [deleteButtonPressed cancelButtonIndex]) { 
     [array removeObject:@"%@", deleteButtonPressed]; 
    } 

ここでの問題タップされた配列内のオブジェクトを、私はこれは自動的%の@を決定しますなかったよう[array removeObject:@"%@", deleteButtonPressed];ではなく、手動で新しい方法を入れています各UIImageViewのボタン(私はそれをやり終わらなければならないかもしれません)。私は "配列"と "deleteButtonPressed"(宣言されていない識別子の使用)に関するエラーを取得している、私は私の人生の代わりに何を置くかを把握することはできません。私はまだ基​​礎を学び、この言語の継承がどのように機能しているのですか?どんな助けや助言も素晴らしいでしょう!私はおそらく関連の継承を示すために、全体のビューコントローラファイルを投稿する必要があります

- (IBAction)grabImage { 
    self.imgPicker = [[UIImagePickerController alloc] init]; 
    self.imgPicker.delegate = self; 
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
     _popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker]; 
     [_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    } 

    else { 
     [self presentModalViewController:imgPicker animated:YES]; 
    } 
    [self.imgPicker resignFirstResponder]; 
} 
// Sets the image in the UIImageView 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { 
    if (imageView.image == nil) { 
     imageView.image = img; 
     [picker dismissModalViewControllerAnimated:YES]; 
     [self.popover dismissPopoverAnimated:YES]; 
     return; 

    } 

    if (imageView2.image == nil) { 
     imageView2.image = img; 
     [picker dismissModalViewControllerAnimated:YES]; 
     [self.popover dismissPopoverAnimated:YES]; 
     return; 
    } 
    if (imageView3.image == nil) { 
     imageView3.image = img; 
     [picker dismissModalViewControllerAnimated:YES]; 
     [self.popover dismissPopoverAnimated:YES]; 
     return; 
    } 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 

    self.user = [NSUserDefaults standardUserDefaults]; 

    NSMutableArray* array = [[self.user objectForKey:@"images"]mutableCopy]; 
    while(array == nil) 
    { 
     [self.user setObject:[NSMutableArray arrayWithObject:@""] forKey:@"images"]; 
     array = [[self.user objectForKey:@"images"]mutableCopy]; 
     NSLog(@"%@",@"attempting to create an array to store the images in"); 
    } 

} 

- (void)applicationDidEnterBackground:(UIApplication*)application { 
    NSLog(@"Image on didenterbackground: %@", imageView); 
    NSMutableArray* array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]]; 

    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]]; 
    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]]; 

      [self.user setObject:array forKey:@"images"]; 
    [user synchronize]; 

      } 

- (void)viewDidLoad 
    { 
     self.user = [NSUserDefaults standardUserDefaults]; 
     NSLog(@"It is %@", self.user); 
     NSMutableArray* array = [[self.user objectForKey:@"images"]mutableCopy]; 
     imageView.image = [[UIImage alloc] initWithData:[array objectAtIndex:0]]; 
     imageView2.image = [[UIImage alloc] initWithData:[array objectAtIndex:1]]; 
     imageView3.image = [[UIImage alloc] initWithData:[array objectAtIndex:2]]; 


     UIApplication *app = [UIApplication sharedApplication]; 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(applicationDidEnterBackground:) 
                name:UIApplicationDidEnterBackgroundNotification 
                object:app]; 

     backToGalleryButton.hidden = YES; 
     tapToDeleteLabel.hidden = YES; 
     deleteButton1.hidden = YES; 
     [super viewDidLoad]; 

    } 

    - (IBAction)deleteButtonPressed:(id)sender { 
    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete" 
                   message:@"Are you sure you want to delete this photo?" 
                  delegate:self 
                cancelButtonTitle:@"Yes" 
                otherButtonTitles:@"No", nil]; 
    [deleteAlertView show]; 

} 

- (void)deleteAlertView:(UIAlertView *)deleteButtonPressed 
     didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (buttonIndex != [deleteButtonPressed cancelButtonIndex]) { 
     [array removeObject:@"%@", deleteButtonPressed]; 
    }  

} 
+1

このコードには多くの問題がありますが、コンソールに投稿された実際のエラーを投稿することはできますか? – sosborn

+0

はい、唯一のエラーは "宣言されていない識別子' array'の使用です。 "副作用と同様に、私のコードで何が間違っていますか? (テーブルビューを使用していない以外の)? – John

答えて

1

問題をここに[array removeObject:@"%@", deleteButtonPressed];

はい、それは問題の一つである(でも、無効な構文を無視して) 。配列にはUIAlertViewが含まれておらず、[user objectForKey:@"images"]に含まれるオブジェクトが含まれています。どのような場合はNSDataインスタンスである必要があり、いずれの場合も間違いなくあなたのUIAlertViewインスタンスではないようです。

つまり、UIAlertViewが対応していると思われるアイテムを魔法のように処理するために、UIAlertViewを配列に渡すことはできません。代わりに、UIAlertViewに、それを作成するときのインデックスに対応するタグを付けます。

UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete" 
                   message:@"Are you sure you want to delete this photo?" 
                  delegate:self 
                cancelButtonTitle:@"Yes" 
                otherButtonTitles:@"No", nil]; 
int imageIndex = <figure out the index of the associated array element based upon 'sender'>; 
deleteAlertView.tag = imageIndex; 

を...と、ボタンが押されたときに、あなたが実行します:あなたはこのような操作を行うことができ

[array removeObjectAtIndex:deleteButtonPressed.tag]; 

そして、その「宣言されていない識別子」の問題を修正するために、あなたはarray中を宣言する必要がありますあなたのヘッダーはviewDidLoadではありません。ローカル変数ではなく、プライベートなインスタンス変数にします。

[[user objectForKey:@"images"] mutableCopy]から要素を削除するとないが自動的に対応する要素が[user objectForKey:@"images"]から削除されることに注意してください。変更を実際に保持したい場合は、変更された配列を[NSUserDefaults standardUserDefaults]に書き戻す必要があります。一つのことをここに絞るが、最初の比較的マイナーなポイントがあります

+0

詳細な回答をいただきありがとうございます。私は今すぐあらゆることに取り組んでいきます。また、変更された配列を '[NSUserDefaults standardUserDefaults]'にどのように書き戻すのか、私の 'deleteAlertViewメソッド?あなたの助けをもう一度ありがとう! – John

+1

あなたは配列の修正が終わった後にそれを行います。そうすれば、 'deleteAlertView'は合理的な場所になります。あなたは '[self.user setObject:array forKey:@" images "]'のようにすることができます。 – aroth

+0

もう一度、皆さんのご協力をお待ちしておりますし、時間をかけて説明していただければ幸いです。 – John

2

- (void)deleteAlertView:(UIAlertView *)deleteButtonPressed didDismissWithButtonIndex:(NSInteger)buttonIndex 

が、それは動詞で終わるという語句「deleteButtonPressedは」も意味します。実際には、オブジェクト、特にUIAlertView型のパラメータを参照しています。 AlertViewのようなものを呼び出す必要があります。

は、第二に、このラインは非常に間違っている:あなたは、文字列を削除しようとしている

[array removeObject:@"%@", deleteButtonPressed]; 

。そのメソッドが引数リスト(コンマで区切られた複数のオブジェクトを渡す)を受け入れた場合、あなたは文字通り "deleteButtonPressed"を削除します。 deleteButtonPressed変数によってポイントされているオブジェクトを削除します。だから、あなたがしなければならないすべては、次のとおりです。

[array removeObject:deleteButtonPressed]; 
+1

はい、 'deleteButtonPressed'は実際に配列の先頭にはありません。 OPが本当にやりたいことは、* 'deleteButtonPressed'に対応する配列要素を削除することです。 – aroth

+1

ロングコードセクションを完全に見逃してしまった(私はそれが何らかの理由であれば私の電話に出ています:))私は今それを通り越して私の答えを編集します –

0

あなたはさまざまな方法ではなく、あなたのdeleteAlertViewメソッドで配列を宣言しているためエラー「宣言されていない識別子配列の使用」を取得しています。私は変数の範囲で読むことをお勧めします。

しかし、基本的な設計上の欠陥がありますので、コードを修正することはできません。

あなたは初心者だと言いますから、このアプリを試す前に読んでいくつかの初心者のチュートリアルを完了することをお勧めします。プロジェクトに参加するのは楽しいことは分かっていますが、アプリケーション設計/エンジニアリングに関しては非常に不満を感じ、悪い習慣を抱くことになります。特に、可変スコープとMVCの設計パターンをより強固に理解しようとします。

+0

誰かが投票して下さったのですか? – sosborn

+0

私はあなたが "質問に答える"のではなく、根本的な問題に取り組んでいるので、あなたが落選したと思います。 /私の目は転がります –

+0

@sosborn私はあなたの答えを投票しました。私はあなたの提案が私にとってかなり役に立ったので誰もがなぜ投票しないのか分かりません。 – John