Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在返回NULL的ViewController之间传递NSString_Ios_Iphone_Objective C_Nsstring - Fatal编程技术网

Ios 在返回NULL的ViewController之间传递NSString

Ios 在返回NULL的ViewController之间传递NSString,ios,iphone,objective-c,nsstring,Ios,Iphone,Objective C,Nsstring,我正在尝试将NSString从一个视图控制器传递到另一个视图控制器。虽然这是一件非常容易的事情,但我不知道我做错了什么,这导致接收到的数据为空。以下是简化代码: DetailViewController.h: @property (nonatomic, strong) NSString *cUrlString; DetailViewController.m @synthesize cUrlString; - (void)tableView:(UITableView *)tableView d

我正在尝试将NSString从一个视图控制器传递到另一个视图控制器。虽然这是一件非常容易的事情,但我不知道我做错了什么,这导致接收到的数据为空。以下是简化代码:

DetailViewController.h:

@property (nonatomic, strong) NSString *cUrlString;
DetailViewController.m

@synthesize cUrlString;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        WebViewController *wvc = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewControllerNew"];
        TheItem *entry = [[channel items] objectAtIndex:[indexPath row]];
        wvc.title = entry.title;
        wvc.urlString = entry.link;
        [self.navigationController pushViewController:wvc animated:YES];

        DetailViewController *cvc = [[DetailViewController alloc] init];
        cvc.cUrlString = entry.link;
        //WHEN I USE NSLOG Below, it gives me the correct URL HERE
        NSLog(@"ENTRY LINK %@", cvc.cUrlString);  

    }

FirstViewController.m

@synthesize cUrlString;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        WebViewController *wvc = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewControllerNew"];
        TheItem *entry = [[channel items] objectAtIndex:[indexPath row]];
        wvc.title = entry.title;
        wvc.urlString = entry.link;
        [self.navigationController pushViewController:wvc animated:YES];

        DetailViewController *cvc = [[DetailViewController alloc] init];
        cvc.cUrlString = entry.link;
        //WHEN I USE NSLOG Below, it gives me the correct URL HERE
        NSLog(@"ENTRY LINK %@", cvc.cUrlString);  

    }
我已经给出了代码的简化版本。还可以看到FirstViewController.m中的NSLog语句显示了正确的数据,但DetailViewController.m的viewDidLoad方法中的日志显示为null。有人能指出我做错了什么吗

        DetailViewController *cvc = [[DetailViewController alloc] init];
        cvc.cUrlString = entry.link;
上面的代码说明了您正在创建DetailViewController的新对象。。 您必须将其显示或推送到导航控制器以显示相同的内容

[self.navigationController pushViewController:cvc animated:YES];

现在,您将拥有带有CUrlString的viewController…

viewDidLoad
是在需要查看
视图时为
UIViewController
实例调用的。当您将其推到导航堆栈上或以模式显示控制器时,会发生这种情况。我不知道为什么
viewDidLoad
方法在
cvc
实例既没有被推送也没有被呈现的情况下被调用

如果指定属性对您不起作用,您可以尝试解决方法。为
DetailViewController
创建自定义
init
方法,如下所示

- (id) initWithURLString:(NSString*) urlString {
    self = [super init];
    if(self) {
        self.cUrlString = urlString;
    }
    return self;
}
DetailViewController.h
中声明此方法

- (id) initWithURLString:(NSString*) urlString;
然后在创建
cvc
实例时使用此init

DetailViewController *cvc = [[DetailViewController alloc] initWithURLString:entry.link];

希望有帮助

您不能尝试在
viewdidappease
viewwillappease
中而不是在
viewDidLoad
中使用字符串属性吗?那会更好

可能是您正在创建视图,当您创建视图控制器或将其添加到堆栈时,会调用其
viewDidLoad
方法/消息

您也可以使用故事板:

我想您没有在创建表视图的位置设置表视图委托

  tableView.delegate = self;
  tableView.dataSource = self;

DetailViewController.m
initWithNibName方法中,像下面这样分配字符串

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

NSLog您传递的字符串
viewdide
不在
viewdideload
上,该字符串在加载时尚未初始化,但在显示时(在加载接口后调用)


我将此作为我的个人经验和研究发表。

看起来不错,让我们来看看非简化版本?您从firstviewcontroller推送细节viewcontroller的位置?显示您正在调用self.navigationController pushViewController:cvc动画:是];在DIDSELECTROWATINDEXTABLEVIEW的didSelectRowAtIndexPath中。并尝试编写cvc.cUrlString=[NSString stringWithFormat:@“%@”,entry.link];entry.link是否为非空?因此问题在于entry.link为null,并将其nil值传递给cvc。cUrlString@AJ112您的意思是要更改导航控制器堆栈上已存在的view controller的值?意味着wvc是另一个类的实例,我正在其中推送一些其他数据。cvc是另一个类的实例,在这个类中,我也想推送字符串。@AJ112我相信cvc和wvc是视图控制器。。。按下此按钮意味着您正在显示这些视图控制器。。。那么你的意思是同时推动。。。。。我的意思是你有两个视图cvc和wvc。。。是不是?@AJ112没问题。。现在您有了“WebViewController”和“DetailsViewController”的两个视图。。。现在你们想什么时候显示这些视图ie,若你们按下控件,就会转到VC按下的viewDidLoad。。。现在您已经在表格行的cellclick上实现了这一点。。。ie是否希望单击单元格时显示ViewController?我是怎么说有两个…一个接一个?我试过这段代码,现在在initWithUrlString中,如果我记录self.cUrlString,它会显示我想要的正确url,但是如果我在viewDidLoad中记录self.cUrlString,它仍然会显示null。为什么会这样?@AJ112您是否正在重置代码中的任何其他位置?因为没有理由让它变成
nil
否则。我重新检查了它,它没有在任何地方重置,也没有在任何地方分配到nil。还有什么问题吗?我想你的代码有点问题,我告诉你另一种方法,你可以这样做,好吧,制作一个常量类,其中包含一个NSString*cUrlString#当您需要使用该字符串时导入,例如首先在FirstViewController中导入,并将该代码简单地放在didSelectRowAtIndexPath CULLSTRING=entry.link上;然后在细节视图控制器中导入常量类,并在您想使用的地方使用cUrlString,我相信它对您有用