在两个ViewControllers之间传递数据问题

时间:2011-06-16 15:54:44

标签: iphone cocoa-touch uiviewcontroller

您好我有两个viewControllers。

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访问?我的错误在哪里?谢谢 的 UPADATE

- (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"];

}

1 个答案:

答案 0 :(得分:0)

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

某处您需要以下内容:

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

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

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