开发者

How release a object with is calling a delegate function to release itself

I think this is simple, but I am having big problem with this.

I have 2 UIViewController开发者_Python百科, one is the "Central" controller and other is for use the camera to decode a barcode.

In Central UIViewController, I call to run the barcode:

   barCode = [[MyBarCode alloc] init]
   [barCode openBarCodeReader];
   //[barCode release];  //can't release it here, this cause problem with camera
}  //function finish

The MyBarCode object will take control of the program. First it create a simple view to UIViewController

- (void) loadView {
    UIView *frame = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [frame setBackgroundColor:[UIColor clearColor]];
    self.view = frame;
}

Than start and configure the BarCode objects and start the camera with a

[self presentModalViewController:barCodeView animated:YES];  

Now, some time later, I have to dismiss this controller

[barCodeView dismissModalViewControllerAnimated:YES];

I send the data with a delegate function, and now this UIViewController finish their job and have to be released. I tried somethings:

  1. Send another delegate message. But, if I do [barCode release]; in this delegate, the program crash, because the barCode object is still running and need go to next instruction

  2. I can send a [self release] but i don't know if it work and where the program will go in the "Central" UIViewController after it.

  3. I can dismiss this aux UIViewController with a

    [self dismissModalViewControllerAnimated:NO];
    

    But again, where the program go in the Central UIViewController.

Anyone have idea to solve this problem?

** Edit **

Maybe I found the answer: I will not call the delegate like a method

[theDelegate BarCodeFinish];

But call it with a notification.

[[NSNotificationCenter defaultCenter] postNotificationName:@"BarCodeFinish" object:nil];

The fist case I still will run the next code after that call, but in the second, the call will run after it finish the function it is inside, and I can release the barcode object with no problem. Is this the best way?


If i understand the second view controller which take control of your program is presented modaly and you looking for a way to dismiss the view controller and release the bar code ?

In my opinion this should be done in a delegate method. For example :

-(void)barCodeReader:(UIViewController *)aViewController didReadBarCode:(NSUInteger)theBarCode;

Write that method in your first view controller, then set it as the delegate of the second. You can even better write your own protocol... But, once the second view controller has read the bar code call your custom method. A way to implement it should be :

-(void)barCodeReader:(UIViewController *)aViewController
      didReadBarCode:(NSUInteger)theBarCode {
    // stop the bar code reader
    [barCode release];
    // dismiss the second view controller
    // do something with theBarCode
}

Once that method returns, your app will wait for the next event, probably some Internet provided data, after sending the code.

I used NSUInteger as the argument but you can set a type of your own. Hope this helps.


Why not just autorelease?

barCode = [[[MyBarCode alloc] init] autorelease];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜