iphone - NSLog 在外部返回 nil (id)

标签 iphone ios objective-c nslog

我正在尝试访问 initWithURL: 之外的 _urlS。如果我在其中使用 NSLog,我会得到一个值,但如果我在 websiteload: 中使用 NSLog,我会得到 (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

最佳答案

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

将分配更改为

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

关于iphone - NSLog 在外部返回 nil (id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15909313/

相关文章:

iphone:如何在 webView 中放置主页按钮

ios - Swift:保存(覆盖)和加载 CoreData

objective-c - 创建一个用计数 N 初始化的 NSArray,都是同一个对象

ios - 当我从 URL 获取文本时,如何删除那些行?

iphone - Facebook SDK 3.1 上的 iOS 6 native 支持问题

iphone - 如何在uiscrollview中滑动图像时添加短声音

ios - Foursquare categoryId 搜索和照片

jquery - 您可以将 JavaScript 和 CSS 文件存储在 localStorage 中以提高在线 Web 应用程序的性能吗?

ios - 单击 UIView 时从 UIView 内部的标签获取文本

ios - 从解析数组中的 25k 行加载并在 TableView 中显示