English 中文(简体)
结业者
原标题:Custom Modal Window with Block Completion Handler

我 st!

我正试图制造一种习俗式的方言。 我愿与国家安全局一道,使用一个编队作为完成手。

我认为,我只需要大刀切。

@implementation ModalWindowController
    - (void)makeKeyAndOrderFront:(id)sender
                   modalToWindow:(NSWindow*)window
                      sourceRect:(NSRect)rect
               completionHandler:(void (^)(NSInteger result))handler {

        _handler = [handler retain];

        session = [NSApp beginModalSessionForWindow:[self window]];
        [[NSApplication sharedApplication] runModalSession:session];

        [[self window] makeKeyAndOrderFrontCentered:self expandingFromFrame:rect];
    }
    - (IBAction)okButtonPressed:(id)sender {
        [[self window] orderOut:self];
        _handler(NSOKButton);
        [NSApp endModalSession:session];
    }

@end

现在,我可以这样说:

[self.modalWindowController makeKeyAndOrderFront:self
                                   modalToWindow:[[self view] window]
                                      sourceRect:sr
                               completionHandler:^(NSInteger result) {
    NSLog(@"Inside Block");
    if ( result == NSOKButton ) {
        // do something interesting here
    }
}];
NSLog(@"Errg");

然而,在方法使KeyAndOrderFront: Format ToWindow:sourceRect:completionHandler: 完工后不会阻挡胎面,因此,即使用户没有选择“ok”或“cancel”,仍将印制“Errg”。 模式窗口是在这个时候展示的,当时用户点击了科索沃,然后又击落了——手脚块。 然而,如果我试图在整块中获取当地变数,而且随着所有物品已经清理完毕,坠毁了。

什么是阻止主线从代号:......方法? 使用区块执行完成制手是否正确?

最佳回答

页 次

_handler=[handler retain];

应当

_handler=[handler copy];

That should solve your problem, that the local variables are gone before the completion handler is called. [handler copy] takes care of the local variables referred to in the block, so that the local variables don t go away even after the flow of the program exited the method where you made the block.

重述以下事实:

  1. The block instance captures the local variables referred inside the block.
  2. However, the block instance is on the stack. It will go away even you retain it, when the flow of the program go out of the scope {...} in which you create the block.
  3. So, you need to copy it, not just retain it, if you want to use the data afterwards, as you are doing here. Copying it automatically retains all the local object variables referred to from the block.
  4. You need to release it once you re done with it. It deallocates the memory for the block itself, and sends release message to the local object variables referred to. If you use GC, you don t have to care about this, though.

为了了解该块的详情,我发现 。 非常有益。

问题回答

暂无回答




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签