4

当按下键盘上的返回按钮时调用一个方法。在调用另一个返回整数的方法后,会根据该整数创建一条消息。然后将消息传递到 UIAlterView 并显示给用户。警报没有任何选项(因此我不调用委托),只是简单地通知用户发生了什么。

编辑:下面是完整的方法(以前显示的部分)。当我注释掉之前的所有内容UIAlertView并替换字符串@“test”而不是传递消息时,警报会成功显示。我的结构没有正确处理内存吗?

- (IBAction)joinButton {
    struct userInfo localUser;

    [emailAddress resignFirstResponder];

    //convert textField text to char array in structure
    localUser.firstName = [self convertStringtoCharArray:firstName.text];
    localUser.lastName = [self convertStringtoCharArray:lastName.text];
    localUser.username = [self convertStringtoCharArray:username.text];
    localUser.email = [self convertStringtoCharArray:emailAddress.text];
    localUser.ipAddress = [self convertStringtoCharArray:localIPAddress.text];
    localUser.latitude = currentLocation.coordinate.latitude;
    localUser.longitude = currentLocation.coordinate.longitude;

    //pass structure to be sent over socket
    int result = [myNetworkConnection registerWithServer:&localUser];

    NSString *message = nil;

    //process result of sending attempt
    if (result == 0) {
        //registration succesful
        message = [NSString stringWithString:@"Registration successful"];
    } else if (result == 1) {
        //server unavailable
        message = [NSString stringWithString:@"Server unavailable. Please check your wi-fi settings and try again."];
    } else if (result == 2) {
        //unable to establish connection
        message = [NSString stringWithString:@"Unable to communicate with server. Please check your wi-fi settings and try again."];
    } else if (result == 3) {
        //username already in use
        message = [NSString stringWithString:@"Username in use. Try another username."];
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration"
                                                    message:message
                                                   delegate:nil 
                                          cancelButtonTitle:@"Ok" 
                                          otherButtonTitles:nil];

    [alert show];
    [alert release];
}

当我执行代码时,iPhone 变灰,就像它即将显示警报但崩溃了。EXC_BAD_ACCESS我在控制台中收到错误。我是否没有正确发布警报或消息?这是控制台输出:

Program received signal:  “EXC_BAD_ACCESS”.
(gdb) backtrace
#0  0x30011944 in objc_msgSend ()
#1  0x3054803e in NSPopAutoreleasePool ()
#2  0x3054c808 in -[NSAutoreleasePool release] ()
#3  0x30936ac4 in _UIApplicationHandleEvent ()
#4  0x3204696c in PurpleEventCallback ()
#5  0x30254a76 in CFRunLoopRunSpecific ()
#6  0x3025416a in CFRunLoopRunInMode ()
#7  0x320452a4 in GSEventRunModal ()
#8  0x308f037c in -[UIApplication _run] ()
#9  0x308eea94 in UIApplicationMain ()
#10 0x000020bc in main (argc=1, argv=0x2ffff508) at /Users/reu2009/Documents/iPhone Development/Development/BuddyTracker/main.m:14
(gdb) frame 10
#10 0x000020bc in main (argc=1, argv=0x2ffff508) at /Users/reu2009/Documents/iPhone Development/Development/BuddyTracker/main.m:14 14       int retVal = UIApplicationMain(argc, argv, nil, nil);

编辑:使用基于答案删除[message release];和分配字符串。[NSString stringWithString];

4

6 回答 6

8

我遇到了这样的问题...我正在从后台线程调用 uiAlertView ....从主线程调用它

于 2012-11-13T17:57:40.807 回答
4

从便利构造函数返回的对象已经设置为自动释放。当你声明了一个指向“message”的指针时,“message”对象本身并不属于你,因为你使用了@"string" 便利构造函数来创建 NSString 对象。因此,您不需要释放它。

当您手动释放它时,它会被释放太多次(一次手动,一次在自动释放过程滚动时)并引发错误。

以下是来自 Apple 的一些附加信息:

http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html

好的经验法则:除非您使用 alloc 或 init 或 copy 方法之一来创建对象(或者如果您自己保留该对象),您不需要释放它,但可以依赖实际创建它的方法来为你做这项工作。

于 2009-07-06T18:48:19.480 回答
1

尝试使用 NSZombieEnabled = YES。

  • 进入可执行文件的信息。
  • 单击“参数”选项卡。
  • 单击“要在环境中设置的变量”上的 +。
  • 键入 NSZombieEnable 和 YES。

当已经释放的内存被释放时,NSZombie会显示地址,然后你就可以使用Instruments找到实际的对象了。Corbin 的 Treehouse 很好地概述了如何执行此操作: Instruments on Leopard: How to debug those random crashs in your Cocoa app

于 2009-07-06T21:09:49.443 回答
0

我在这里使用 UIAlertView 遇到了同样的问题,就我而言,我有另一个实现警报的类,并且从另一个类中我调用了一个静态方法。如下所示:

ClassA

...

doSomething {
 ... some stuff ...

 [MyAlertView showAlert];

 ... some other stuff...

}

我怀疑的是,当我单击该对象已释放的按钮时,警报视图是异步显示的。

为了验证这一点,我更改了代码以实例化警报而不是释放它。一切都奏效了。

我的最终解决方案是在父视图中声明一个变量,并在视图被释放时将其与其他变量一起释放。

于 2010-03-29T20:02:53.243 回答
0

Sean 是对的——您不需要在这里调用 [message release],因为您实际上从未保留消息对象。

而不是只是说message = @"string",你需要说message = [NSString stringWithString:@"string"];老实说,我不知道为什么(也许有人可以发表评论,我可以改进这篇文章!)但这应该可以解决问题。

于 2009-07-06T19:03:04.317 回答
0

这可能是由于从后台线程更新 UIKit

我这样解决了

UIAlertView *alertMSG = [[UIAlertView alloc] initWithTitle:nil
                        message:@"Your mnessage here"
                        delegate:self
                        cancelButtonTitle:@"Title here"
                        otherButtonTitles: nil];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
         [alertMSG show];
    }];
于 2014-03-08T12:13:24.660 回答