ios - 使用委托(delegate)清理数组
<p><p>我制作了一个 AR 应用程序,它可以识别图像并显示在 AlertView 中识别的对象。在 AlertView 中,我有 2 个按钮:添加和取消,我使用 <code>UIAlertViewDelegate</code> 来了解用户按下了哪个按钮。如果用户按下添加按钮,识别的对象将存储在一个数组中。我将此数组传递给另一个 ViewController,我在其中设置了一个 TableView。在这个 TableView 的底部有一个“支付”按钮可以转到另一个 ViewController,我在其中显示识别的对象的总价格。从最后一个 ViewController 我可以按下一个按钮来支付我使用 AR 选择的对象。现在,当我按下此按钮时,应用程序将关闭此 ViewController 并返回到第一个 ViewController,但我存储 AR 识别的对象的数组已满。要删除这个数组的内容,我认为最好的方法是使用委托(delegate)方法,所以我做了这个:</p>
<p><strong>PaymentViewController.h</strong></p>
<pre><code>#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
</code></pre>
<p><strong>PaymentViewController.m</strong></p>
<pre><code>#import "PaymentViewController.h"
@interface PaymentViewController () <UIAlertViewDelegate>
@end
@implementation PaymentViewController
@synthesize delegate = _delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
;
self.labelTotal.text = self.total;
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonClosePaymentVC:(id)sender {
;
}
- (IBAction)buttonPay:(id)sender {
NSString *pay = ;
UIAlertView *alert = [ initWithTitle:@"HelloMS" message:pay delegate:self cancelButtonTitle:@"Si" otherButtonTitles:@"No", nil];
;
}
- (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 = ;
NSString *path = ;
NSError *error;
if (![removeItemAtPath:path error:&error]) {
NSLog(@"Errore: %@", error);
}
__weak UIViewController *vcThatPresentedCurrent = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
;
}];
;
}
if (buttonIndex == 1) {
// Non deve far nulla: fa scomparire l'UIAlertView
}
}
</code></pre>
<p>在这里,我将使用委托(delegate)的类的方法发布给您:</p>
<p><strong>ScannerViewController.m 的接口(interface)</strong></p>
<pre><code>@interface ScannerViewController () <MSScannerSessionDelegate, PaymentViewControllerDelegate, UIActionSheetDelegate, UIAlertViewDelegate>
@property (weak) IBOutlet UIView *videoPreview;
- (IBAction)stopScanner:(id)sender;
@end
</code></pre>
<p>在 <code>ViewDidLoad</code> 我插入了这些行:</p>
<pre><code>PaymentViewController *pay = [init];
;
</code></pre>
<p>并且在 <code>ScannerViewController.m</code> 我实现了我在 <code>PaymentViewController.h</code> 中声明的方法:</p>
<pre><code>- (void)cleanReportArray {
;
}
</code></pre>
<p>我在我的 iPhone 上测试了我的应用程序,该应用程序运行良好,直到我尝试支付我通过相机扫描的对象,事实上,我尝试支付对象,但它没有清理我存储对象的数组扫描。
我的代码有什么问题?我使用网络上的教程来更好地理解委托(delegate)方法的工作原理。希望能帮我解决这个问题,谢谢</p>
<p><strong>更新:</strong>
在这里我将发布我的 ScannerViewController 代码:</p>
<p><strong>ScannerViewController.h</strong></p>
<pre><code>#import <UIKit/UIKit.h>
@interface ScannerViewController : UIViewController
@end
</code></pre>
<p><strong>ScannerViewController.m</strong></p>
<pre><code>#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 = ;
if (self) {
_scannerSession = [ initWithScanner:];
;
;
}
return self;
}
- (void)session:(MSScannerSession *)scanner didScan:(MSResult *)result {
if (!result) {
return;
}
;
NSString *resultStr = nil;
if (result) {
switch () {
case MS_RESULT_TYPE_IMAGE:
resultStr = ];
break;
case MS_RESULT_TYPE_EAN8:
case MS_RESULT_TYPE_EAN13:
resultStr = ];
break;
default:
break;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
UIActionSheet *asView = [initWithTitle:resultStr delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
asView.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
;
;
});
}
- (void)addObjectToList:(NSString *)objectName {
// Ricerca dell'oggetto
NSString *object = ;
if () {
;
}
if () {
;
}
if () {
;
}
if () {
;
}
}
- (void)showAlert:(NSString*)name {
name = ;
nameOfObjectScanned = name;
NSString *message = ;
UIAlertView *alert = [ initWithTitle:@"HelloMS" message:message delegate:self cancelButtonTitle:@"Aggiungi" otherButtonTitles:@"Annulla", nil];
;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(@"Aggiungi");
for (int i = 0; i < ; i++) {
if ([objectForKey:@"name"] isEqualToString:nameOfObjectScanned]) {
// Salvo il nome dell'oggetto trovato, il prezzo e la descrizione
NSString *name = objectForKey:@"name"];
NSString *desc = objectForKey:@"desc"];
NSString *price = objectForKey:@"price"];
NSDictionary *newObjectAdded = [init];
newObjectAdded = @{@"name": name,
@"desc": desc,
@"price": price};
;
}
}
} else {
NSLog(@"Annulla");
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
;
}
- (void)viewDidLoad
{
;
arrayObjectAdded = [init];
CALayer *videoPreviewLayer = ;
;
CALayer *captureLayer = ;
];
objectAtIndex:0]];
reader = [init];
;
;
PaymentViewController *pay = [init];
;
}
- (void)cleanReportArray {
;
}
- (void)dealloc {
;
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
- (IBAction)stopScanner:(id)sender {
ReportViewController *reportVC = [initWithNibName:@"ReportViewController" bundle:nil];
reportVC.reportArray = arrayObjectAdded;
;
;
}
@end
</code></pre>
<p>为了识别图片我正在使用这个<a href="https://moodstocks.com" rel="noreferrer noopener nofollow">AR SDK</a> .我希望你能帮助我了解我的问题在哪里</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你的问题是在 <code>viewDidLoad</code> 你有代码:</p>
<pre><code>PaymentViewController *pay = [init];
;
</code></pre>
<p>这是您在该方法中做的最后一件事。因此,您创建并设置委托(delegate)的 <code>PaymentViewController</code> 实例会立即被销毁(由 ARC)。</p>
<p>您需要修改代码,以便在屏幕上显示的 <code>PaymentViewController</code> 的实际实例上调用 <code>setDelegate:</code>,因为这是需要使用委托(delegate)(它从警报 View 接收回调)。</p></p>
<p style="font-size: 20px;">关于ios - 使用委托(delegate)清理数组,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19490968/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19490968/
</a>
</p>
页:
[1]