【问题标题】:Passing data between two ViewControllers Problem在两个 ViewController 之间传递数据问题
【发布时间】:2011-06-16 15:54:43
【问题描述】:

您好,我有两个 viewController。

NewsViewController

#import "SingleViewController.h"
@interface NewsViewController : UIViewController {
}
- (NSString *)getFirstImage:(NSString *)htmlString;
@end

以及实现文件:

#import "SingleViewController.h"
SingleViewController *singleViewController;

@interface NewsPadViewController () <UIActionSheetDelegate>

- (void)loadSubscriptions;

@property (nonatomic, assign) BOOL wrap;
@property (nonatomic, retain) NSMutableArray *items;

@end

@implementation NewsPadViewController
....CODE....
- (void)carouselCurrentItemTapped{
    //[self addSubview:articolo];
    MWFeedItem *item =[reader.feedItems objectAtIndex:[carousel currentItemIndex]];

    NSLog(@"Current Item tappped, index: %d", [carousel currentItemIndex]);

    singleViewController.prova.text=@"CIAO";
    singleViewController.ciao=@"sample";
    [singleViewController.web loadHTMLString:item.summary baseURL:nil];
    NSLog(@"conternutio:");
    NSLog(singleViewController.ciao);
}

SingleViewController.h

#import <UIKit/UIKit.h>

@interface SingleViewController : UIViewController{
    @public
    UIWebView *web;
    UILabel *prova;
    NSString *ciao;
}
@property (nonatomic,retain) IBOutlet UIWebView *web;
@property (nonatomic,retain) IBOutlet UILabel *prova;
@property (nonatomic,retain) IBOutlet NSString *ciao;
@end

和 SingleViewController.m

#import "SingleViewController.h"

@implementation SingleViewController
@synthesize web,prova,ciao;

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

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [prova setText:ciao];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

为什么我不能从 SingleViewController 中的对象的 NewsPadViewController 访问?我的错误在哪里?谢谢 更新

- (void)carouselCurrentItemTapped{
    //[self addSubview:articolo];
    MWFeedItem *item =[reader.feedItems objectAtIndex:[carousel currentItemIndex]];

    NSLog(@"Current Item tappped, index: %d", [carousel currentItemIndex]);

SingleViewController *singleViewController  = [[SingleViewController alloc] initWithNibName:@"SingleViewController" bundle:[NSBundle mainBundle]];    

    singleViewController.prova.text=@"CIAO";
    singleViewController.ciao=@"sample";
    [singleViewController.web loadHTMLString:item.summary baseURL:nil];
    NSLog(@"conternutio:");
    NSLog(singleViewController.ciao);
    [singleViewController setOpz:@"sample"];

}

【问题讨论】:

    标签: iphone cocoa-touch uiviewcontroller


    【解决方案1】:

    我没有看到 SingleViewController 的任何实例化。您有一个指向它的指针,但我在您的代码示例中没有看到您实际创建它的任何地方。

    在某个地方你需要类似的东西:

    if (!singleViewController) {
        singleViewController = [[SingleViewController alloc] init];
    }
    

    SingleViewController *singleViewController = [[SingleViewController alloc] initWithNibName:@"SingleView" bundle:nil];
    

    或者您需要从其他地方发送消息或在 NewsViewController 中设置一个实例变量,该变量指向现有的 SingleViewController 实例。

    【讨论】:

    • 感谢我添加了代码的第一部分,并使用 NSLog(singleViewController.ciao) 工作!但在xib中不起作用! ciao NSString 没有加载到 UILabel 中,UIWebView 什么也没有!
    • 听起来你没有在 Interface Builder 中设置插座
    • 网点是正确的!我尝试通过 viewDidLoad 方法在 UILabel 中分配文本,它可以工作!问题仅在于我在 newsPadViewController 中分配的那个刺痛!事实上,如果我从 SingleViewController 中的 viewDidLoad 尝试 NSLog(ciao),则字符串为空,而如果我尝试从 NewsPadViewController 尝试 NSLog(SingleViewController.ciao),它包含文本“sample”
    猜你喜欢
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2016-03-24
    • 2017-01-22
    相关资源
    最近更新 更多