NSLog返回nil outside(id)

时间:2013-04-09 18:06:53

标签: iphone ios objective-c nslog

我正在尝试访问_urlS之外的initWithURL:。如果我在其中的NSLog,我得到一个值,但如果我NSLog websiteload:,我得到(null)

真的很奇怪,有人有什么想法吗?

.h文件:

@interface WebsiteViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate, MFMailComposeViewControllerDelegate, UIWebViewDelegate, UITextFieldDelegate> {
    UIPopoverController *popoverController;
    UIBarButtonItem *barButton;

    NSString *urlWeb;

    IBOutlet UIWebView *Website;

    UILabel *testLabel;

    IBOutlet UIButton *btn;

    IBOutlet UILabel *lbllong;

    IBOutlet UIActivityIndicatorView *activityIndicator;
}

@property (nonatomic, retain) IBOutlet UIPopoverController *popoverController;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *barButton;
@property (nonatomic, retain) NSString *urlS;

- (void)addBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc;
- (void)removeBarButtonItem;

- (id)initWithURL:(NSString *)postURL;


@end

.m文件:

#import "WebsiteViewController.h"

@interface WebsiteViewController ()

@end

@implementation WebsiteViewController

@synthesize barButton, popoverController;
@synthesize urlS = _urlS;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithURL:(NSString *)postURL
{
    self = [super init];
    if (self) {
        _urlS = [[postURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] retain];
        [lbllong setText:_urlS];
    }
    return self;
}

-(IBAction)websiteload:(id)sender {

    NSLog(@"%@", _urlS);


}

- (void)addBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {

}
- (void)removeBarButtonItem {

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:3)

分配时需要保留对象,因为 stringByTrimmingCharactersInSet:返回一个自动释放的实例,因此它会被释放,因为你不拥有它。

将作业更改为

_urlS = [[postURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] retain];