2012-06-25 217 views
0

我使我的老虎機應用程序使用iCarousel,我的iCarousel包含圖像來自NSDocumentDirectory此圖像來自我的ImagePicker。所以這裏是我的應用程序如何工作,當用戶按下按鈕iCarousel旋轉。 停止時,顯示該項目3秒鐘,然後將其刪除。NSMutableArray索引刪除

我的問題是當我去另一個視圖時,刪除的索引/項目再次出現。即使我看到不同的觀點,如何保持我的陣列。刪除的索引/項目將不會顯示,直到重新啓動應用程序爲止,例如保存數組。謝謝您的幫助。

//我的數組

- (void)viewDidLoad 
{ 
    self.images = [NSMutableArray new]; 
    for(int i = 0; i <= 100; i++) 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDir = [paths objectAtIndex:0]; 

     NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"images%d.png", i]]; 
      if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
       [images addObject:[UIImage imageWithContentsOfFile:savedImagePath]]; 
      } 
    } 
} 



- (void)viewWillAppear:(BOOL)animated {  
    spinButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [spinButton addTarget:self action:@selector(spin) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:spinButton]; 
} 
- (void) carouselDidEndScrollingAnimation:(iCarousel *)carousel{ 
    [NSTimer scheduledTimerWithTimeInterval:0.56 //this arranges the duration of the scroll 
            target:self 
            selector:@selector(deleteItem) 
            userInfo:nil 
            repeats:NO]; 
} 

//自旋和刪除方法

- (void)spin { 
     [carousel scrollByNumberOfItems:-35 duration:10.7550f]; 
} 

-(void) deleteItem { 
    //Removes the object chosen 
     NSInteger index = carousel.currentItemIndex; 
     [carousel removeItemAtIndex:index animated:YES]; 
     [images removeObjectAtIndex:index]; 
} 

我需要的是,當索引/項目被刪除,不會被暫時就算我出去其他意見。只有在應用程序關閉並重新打開後,視圖纔會重新啓動。

+0

您在哪裏創建這個數組,即在哪個方法中以及何時從數組中刪除元素? – rishi

+0

已編輯它,請看看。 – Bazinga

+0

如果您不是每次都創建視圖,則需要在viewWillAppear中重現數組。否則,您只能爲這兩個視圖使用一個數組,並且可以在應用程序委託中定義該數組。更簡單的方法:) – DivineDesert

回答

1

你的問題是你每次進入視圖時都在創建圖像NSMutableArray

由於@Saleh說你應該把數組放在視圖控制器之外。要做到這一點在appDelegate,像他建議,做到以下幾點:

AppDelegate.h聲明:

@property(strong, nonatomic) NSMutableArray *images; 

AppDelegate.m

@synthesize images; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Read the images after the existing code .... 

    self.images = [NSMutableArray array]; 
    for(int i = 0; i <= 100; i++) 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDir = [paths objectAtIndex:0]; 

     NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"images%d.png", i]]; 
     if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
      [images addObject:[UIImage imageWithContentsOfFile:savedImagePath]]; 
     } 
    }  

    return YES; 
} 

然後在您的ViewController.m

#import "AppDelegate.h" 

,改變你的viewDidLoad方法:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.images = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).images; 
} 

這應該工作。

+0

我需要的是,當索引/項目被刪除時,即使我轉到其他視圖也不會臨時顯示。視圖只會在應用程序關閉並重新打開後重新啓動。 – Bazinga

+0

是的,我知道,這是你可以用這個答案實現的。你試過了嗎? –

+0

是的,仍然不起作用 – Bazinga

0

使用數組的一個實例,只初始化它一次。將數組放在appDelegate中,這將使整個應用程序成爲singelton。

+0

你能給我一些片段嗎? – Bazinga

+0

只需在appDelegate中添加數組。使它成爲applicationDidFinishLaunching中的屬性,初始/內存分配,並通過屬性訪問它並將其用於視圖中。 – Saleh

+0

將它添加到applicationDidFinishLaunching中,使其成爲一個屬性,但是當我在CarouselView中使用它時,沒有任何內容出現。 – Bazinga