2013-10-21 62 views
2

我製作了AR應用程序,可識別圖像並顯示在AlertView中識別的對象。在AlertView中,我有2個按鈕:添加和取消,我使用UIAlertViewDelegate來了解用戶按下了哪個按鈕。如果用戶按下添加按鈕,則識別的對象將被存儲在一個數組中。我將這個數組傳遞給另一個ViewController,我在其中設置了一個TableView。在這個TableView的底部有一個按鈕「支付」去另一個ViewController,我在其中顯示識別的對象的總價格。從最後一個ViewController中,我可以按下一個按鈕來支付使用AR選擇的對象。現在當我按下這個按鈕時,應用程序關閉這個ViewController並返回到第一個ViewController,但是我存儲了AR識別它已滿的對象的數組。要刪除這個數組的內容我認爲最好的方法是使用代理使用的方法,所以我做了這個:使用委託清潔陣列

PaymentViewController.h

#import <UIKit/UIKit.h> 

@protocol PaymentViewControllerDelegate; 

@interface PaymentViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UILabel *labelTotal; 
- (IBAction)buttonClosePaymentVC:(id)sender; 

- (IBAction)buttonPay:(id)sender; 

@property(nonatomic,strong)NSString *total; 

@property(assign) id<PaymentViewControllerDelegate> delegate; 

@end 

@protocol PaymentViewControllerDelegate <NSObject> 

- (void)cleanReportArray; 

@end 

PaymentViewController.m

#import "PaymentViewController.h" 

@interface PaymentViewController() <UIAlertViewDelegate> 

@end 

@implementation PaymentViewController 
@synthesize delegate = _delegate; 

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

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.labelTotal.text = self.total; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)buttonClosePaymentVC:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (IBAction)buttonPay:(id)sender { 
    NSString *pay = [NSString stringWithFormat:@"Stai per pagare %@, procedi?", self.total]; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"HelloMS" message:pay delegate:self cancelButtonTitle:@"Si" otherButtonTitles:@"No", nil]; 
    [alert show]; 
} 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     // Procedura per il pagamento e cancellazione del file plist 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"objects.plist"]; 
     NSError *error; 
     if (![[NSFileManager defaultManager]removeItemAtPath:path error:&error]) { 
      NSLog(@"Errore: %@", error); 
     } 
     __weak UIViewController *vcThatPresentedCurrent = self.presentingViewController; 
     [self dismissViewControllerAnimated:YES completion:^{ 
      [vcThatPresentedCurrent dismissViewControllerAnimated:YES completion:nil]; 
     }]; 
     [self.delegate cleanReportArray]; 
    } 
    if (buttonIndex == 1) { 
     // Non deve far nulla: fa scomparire l'UIAlertView 
    } 
} 

在這裏,我發佈給您將使用代表的類的方法:

所述ScannerViewController.m的

接口

@interface ScannerViewController() <MSScannerSessionDelegate, PaymentViewControllerDelegate, UIActionSheetDelegate, UIAlertViewDelegate> 
@property (weak) IBOutlet UIView *videoPreview; 
- (IBAction)stopScanner:(id)sender; 
@end 

ViewDidLoad我插入此行:

PaymentViewController *pay = [[PaymentViewController alloc]init]; 
[pay setDelegate:self]; 

而在ScannerViewController.m我實現我在PaymentViewController.h聲明的方法:

- (void)cleanReportArray { 
    [arrayObjectAdded removeAllObjects]; 
} 

我在iPhone上測試了我的應用程序,該應用程序適用於fi ne直到我試圖支付由相機掃描的對象,實際上,我試圖支付該對象,但它不會清理存儲掃描對象的陣列。 我的代碼有什麼問題?我在網上使用了一個教程,以更好地理解授權方法的工作原理。我希望你能幫助我解決這個問題,謝謝

UPDATE: 在這裏,我將張貼我的ScannerViewController代碼:

ScannerViewController.h

#import <UIKit/UIKit.h> 

@interface ScannerViewController : UIViewController 

@end 

ScannerViewController.m

#import "ScannerViewController.h" 
#import "PaymentViewController.h" 
#import "ReportViewController.h" 
#import "MSScannerSession.h" 
#import "MSResult.h" 
#import "XmlReader.h" 

static int kMSScanOptions = MS_RESULT_TYPE_IMAGE | 
          MS_RESULT_TYPE_EAN8  | 
          MS_RESULT_TYPE_EAN13; 

@interface ScannerViewController() <MSScannerSessionDelegate, PaymentViewControllerDelegate, UIActionSheetDelegate, UIAlertViewDelegate> 
@property (weak) IBOutlet UIView *videoPreview; 
- (IBAction)stopScanner:(id)sender; 
@end 

@implementation ScannerViewController { 
    MSScannerSession *_scannerSession; 
    NSString *nameOfObjectScanned; 
    XmlReader *reader; 
    NSMutableArray *arrayObjectAdded; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     _scannerSession = [[MSScannerSession alloc] initWithScanner:[MSScanner sharedInstance]]; 
     [_scannerSession setScanOptions:kMSScanOptions]; 
     [_scannerSession setDelegate:self]; 

    } 
    return self; 
} 

- (void)session:(MSScannerSession *)scanner didScan:(MSResult *)result { 
    if (!result) { 
     return; 
    } 
    [_scannerSession pause]; 

    NSString *resultStr = nil; 

    if (result) { 
     switch ([result getType]) { 
      case MS_RESULT_TYPE_IMAGE: 
       resultStr = [NSString stringWithFormat:@"Immagine trovata: %@", [result getValue]]; 
       break; 
      case MS_RESULT_TYPE_EAN8: 
      case MS_RESULT_TYPE_EAN13: 
       resultStr = [NSString stringWithFormat:@"EAN trovato: %@", [result getValue]]; 
       break; 
      default: 
       break; 
     } 
    } 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     UIActionSheet *asView = [[UIActionSheet alloc]initWithTitle:resultStr delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:nil otherButtonTitles:nil, nil]; 
     asView.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 
     [asView showInView:self.view]; 
     [self addObjectToList:resultStr]; 
    }); 

} 

- (void)addObjectToList:(NSString *)objectName { 
    // Ricerca dell'oggetto 
    NSString *object = [objectName substringFromIndex:18]; 
    if ([object isEqualToString:@"Binario_con_coppia"]) { 
     [self showAlert:object]; 
    } 
    if ([object isEqualToString:@"Dadi_colorati"]) { 
     [self showAlert:object]; 
    } 
    if ([object isEqualToString:@"Dadi_rossi"]) { 
     [self showAlert:object]; 
    } 
    if ([object isEqualToString:@"Bici_da_corsa"]) { 
     [self showAlert:object]; 
    } 
} 

- (void)showAlert:(NSString*)name { 
    name = [name stringByReplacingOccurrencesOfString:@"_" withString:@" "]; 
    nameOfObjectScanned = name; 
    NSString *message = [NSString stringWithFormat:@"Ho riconosciuto questo oggetto: %@, vuoi aggiungerlo al carrello?", name]; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"HelloMS" message:message delegate:self cancelButtonTitle:@"Aggiungi" otherButtonTitles:@"Annulla", nil]; 
    [alert show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     NSLog(@"Aggiungi"); 
     for (int i = 0; i < [reader.objArray count]; i++) { 
      if ([[reader.objArray[i]objectForKey:@"name"] isEqualToString:nameOfObjectScanned]) { 
       // Salvo il nome dell'oggetto trovato, il prezzo e la descrizione 
       NSString *name = [reader.objArray[i]objectForKey:@"name"]; 
       NSString *desc = [reader.objArray[i]objectForKey:@"desc"]; 
       NSString *price = [reader.objArray[i]objectForKey:@"price"]; 
       NSDictionary *newObjectAdded = [[NSDictionary alloc]init]; 
       newObjectAdded = @{@"name": name, 
            @"desc": desc, 
            @"price": price}; 
       [arrayObjectAdded addObject:newObjectAdded]; 
      } 
     } 
    } else { 
     NSLog(@"Annulla"); 
    } 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    [_scannerSession resume]; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    arrayObjectAdded = [[NSMutableArray alloc]init]; 
    CALayer *videoPreviewLayer = [self.videoPreview layer]; 
    [videoPreviewLayer setMasksToBounds:YES]; 

    CALayer *captureLayer = [_scannerSession previewLayer]; 
    [captureLayer setFrame:[self.videoPreview bounds]]; 

    [videoPreviewLayer insertSublayer:captureLayer below:[[videoPreviewLayer sublayers] objectAtIndex:0]]; 
    reader = [[XmlReader alloc]init]; 
    [reader parseXml]; 
    [_scannerSession startCapture]; 
    PaymentViewController *pay = [[PaymentViewController alloc]init]; 
    [pay setDelegate:self]; 
} 

- (void)cleanReportArray { 
    [arrayObjectAdded removeAllObjects]; 
} 

- (void)dealloc { 
    [_scannerSession stopCapture]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)stopScanner:(id)sender { 
    ReportViewController *reportVC = [[ReportViewController alloc]initWithNibName:@"ReportViewController" bundle:nil]; 
    reportVC.reportArray = arrayObjectAdded; 
    [reportVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; 
    [self presentViewController:reportVC animated:YES completion:nil]; 
} 

@end 

要識別圖片,我正在使用這個AR SDK。我希望你能幫助我明白的地方是我的問題

+0

當您調用'cleanReportArray'時,您是否做過任何調試以檢查委託是否已設置? – Wain

+0

我現在檢查了一下:我在這一行上放了一個斷點:'[pay setDelegate:self];',當我運行應用程序時,我看到在「付費」下設置了代表... – lucgian84

+0

而且當你來使用委託?你的代碼通常看起來是正確的,所以你可能會設置委託在錯誤的對象或類似的東西... – Wain

回答

1

你的問題是,在viewDidLoad你的代碼:

PaymentViewController *pay = [[PaymentViewController alloc]init]; 
[pay setDelegate:self]; 

這是你在該方法中做的最後一件事。因此,您創建並設置委託的PaymentViewController實例會立即銷燬(由ARC)。

您需要修改代碼,以便您撥打setDelegate:上呈現在屏幕上的PaymentViewController的實際情況,因爲這是一個需要使用委託(它接收來自警報視圖回調)的實例。

+0

謝謝我現在就試試! – lucgian84