iPhone内存管理,视图之间的EXC_BAD_ACCESS NSString

发布于 2024-12-09 17:33:01 字数 1745 浏览 0 评论 0原文

我在内存管理方面遇到一些问题,我认为我的问题是分配和对象释放。我想将一个 NSString (userID) 值分配给其他视图中的另一个 NSString (user_id)。

MainViewController.h

#import "OnlineCheckViewController.h"

@interface MainViewController : UIViewController
{
    NSString *userID;
}
@end

MainViewController.m

#import "MainViewController.h"

- (IBAction)OnlineCheck:(id)sender
{
    OnlineCheckViewController *controller = [[OnlineCheckViewController alloc] initWithNibName:@"OnlineCheckViewController" bundle:nil];
    controller.delegate = self;

    controller.user_id = userID;

    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:controller animated:YES];

    [controller release];
}
- (void)viewDidUnload
{
    userID = nil;
    [super viewDidUnload];
}

- (void)dealloc
{
    [userID release];
    [super dealloc];
}

OnlineCheckViewController.h

#import <UIKit/UIKit.h>
#import "OnlineCheckResultsViewController.h"

@protocol OnlineCheckViewControllerDelegate;

@interface OnlineCheckViewController : UIViewController 
     <OnlineCheckResultsViewControllerDelegate>
{
    NSString *user_id;
}

@property (nonatomic, assign) id <OnlineCheckViewControllerDelegate> delegate;
@property (nonatomic, retain) NSString *user_id;

@end

OnlineCheckViewController.m

#import "OnlineCheckViewController.h"

@synthesize user_id;
@synthesize delegate=_delegate;

- (void)dealloc
{
    [user_id release];
    [super dealloc];
}
- (void)viewDidUnload
{
    [self setUser_id:nil];
    [super viewDidUnload];
}

谢谢!

I have some problems with memory management, I think my problem is the assignment, and object releases. I want to assign a NSString (userID) value to another NSString in other view (user_id).

MainViewController.h

#import "OnlineCheckViewController.h"

@interface MainViewController : UIViewController
{
    NSString *userID;
}
@end

MainViewController.m

#import "MainViewController.h"

- (IBAction)OnlineCheck:(id)sender
{
    OnlineCheckViewController *controller = [[OnlineCheckViewController alloc] initWithNibName:@"OnlineCheckViewController" bundle:nil];
    controller.delegate = self;

    controller.user_id = userID;

    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:controller animated:YES];

    [controller release];
}
- (void)viewDidUnload
{
    userID = nil;
    [super viewDidUnload];
}

- (void)dealloc
{
    [userID release];
    [super dealloc];
}

OnlineCheckViewController.h

#import <UIKit/UIKit.h>
#import "OnlineCheckResultsViewController.h"

@protocol OnlineCheckViewControllerDelegate;

@interface OnlineCheckViewController : UIViewController 
     <OnlineCheckResultsViewControllerDelegate>
{
    NSString *user_id;
}

@property (nonatomic, assign) id <OnlineCheckViewControllerDelegate> delegate;
@property (nonatomic, retain) NSString *user_id;

@end

OnlineCheckViewController.m

#import "OnlineCheckViewController.h"

@synthesize user_id;
@synthesize delegate=_delegate;

- (void)dealloc
{
    [user_id release];
    [super dealloc];
}
- (void)viewDidUnload
{
    [self setUser_id:nil];
    [super viewDidUnload];
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱世争霸 2024-12-16 17:33:01
@interface MainViewController : UIViewController
{
    NSString *userID;
}

我敢打赌,这就是你的问题。请注意,当您创建属性“user_id”时,您指定“retain”。保留可确保处理器在您指示之前不会释放字符串。

NSString 会自动设置为自动释放。如果你打算将值保留在非属性 NSString (userID) 中,那么你需要调用 [userID keep];设置它的值后。

我猜测您从未保留过 userID,并且当您尝试将 user_id 设置为它时,它会调用错误或更可能,它会抛出错误,因为您在 dealloc 中释放了它。

@interface MainViewController : UIViewController
{
    NSString *userID;
}

I'm going to bet that this is your problem. Note that when you create the property "user_id", you specify "retain". Retain makes sure that the processor doesn't release the string until you tell it to.

An NSString is automatically set to auto release. If you plan to keep the value in the non property NSString (userID) then you need to call [userID retain]; after you set the value of it.

I'm guessing that you never retained userID and when you try to set user_id to it, it calls the error or more likely, it throws the error because you release it in dealloc.

魂牵梦绕锁你心扉 2024-12-16 17:33:01

感谢您的所有回答。

我已经解决了这个问题。

MainViewController.h

,我添加了 userID IBOutlet

@property (nonatomic, retain) IBOutlet NSString *userID;

MainViewController.m

@synthesize userID;

- (IBAction)OnlineCheck:(id)sender
{
controller.user_id = self.userID;
}

- (void)viewDidUnload
{
self.userID = nil;
}

- (void)dealloc
{
[userID release];
}

中 在 OnlineCheckViewController.h

更改为分配

@property (nonatomic, assign) NSString *user_id;

OnlineCheckViewController.m 中强>

- (void)dealloc
{
// removed
// [user_id release];
}

- (void)viewDidUnload
{
// removed
// [self setUser_id:nil];
}

Thanks to all your answers.

I have resolved this issue.

In MainViewController.h

I have added userID IBOutlet

@property (nonatomic, retain) IBOutlet NSString *userID;

In MainViewController.m

@synthesize userID;

- (IBAction)OnlineCheck:(id)sender
{
controller.user_id = self.userID;
}

- (void)viewDidUnload
{
self.userID = nil;
}

- (void)dealloc
{
[userID release];
}

In OnlineCheckViewController.h

Changed to assign

@property (nonatomic, assign) NSString *user_id;

In OnlineCheckViewController.m

- (void)dealloc
{
// removed
// [user_id release];
}

- (void)viewDidUnload
{
// removed
// [self setUser_id:nil];
}
得不到的就毁灭 2024-12-16 17:33:01

尝试这个代码

- (IBAction)OnlineCheck:(id)sender
{
    OnlineCheckViewController *controller = [[OnlineCheckViewController alloc] initWithNibName:@"OnlineCheckViewController" bundle:nil];
    controller.delegate = self;

    controller.user_id = userID;
    [controller.user_id retain];
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:controller animated:YES];

    [controller release];
}

我想这可以解决你的问题......

Try this code

- (IBAction)OnlineCheck:(id)sender
{
    OnlineCheckViewController *controller = [[OnlineCheckViewController alloc] initWithNibName:@"OnlineCheckViewController" bundle:nil];
    controller.delegate = self;

    controller.user_id = userID;
    [controller.user_id retain];
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:controller animated:YES];

    [controller release];
}

i think this may solve your problem...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文