簡體   English   中英

使用StoryBoards在Segue中傳遞信息

[英]Passing Information in Segue Using StoryBoards

我已經花了一段時間來嘗試使用iOS應用程序,但實際上並沒有太多時間來投資它。 我現在正把頭撞在牆上,因為我不知道如何使它正常工作或配置不正確...我正在嘗試獲取NewsTableViewController中引用的正文以加載到NewsView的textView中控制器。 目前,只有標題更新,並且textview僅顯示lorum ipsum文本。

我的理解是,因為我可以在NSLog中看到信息,並且如果我嘗試將新聞正文文本放在被推送的視圖的標題中,則該消息會顯示在標題中-我的想法是我未能定義視圖,但正如我所說我只是看不到! 反正這就是我所擁有的...

這是將數據從xml文件加載到第一個視圖的表

//
//  NewsTableViewController.h
//

#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"

@interface NewsTableViewController : UITableViewController
{
    IBOutlet UITableView *newsTable;
    CGSize cellSize;
    NSXMLParser *rssParser;
    NSMutableArray *stories;
    NSMutableDictionary *item;
    NSString *currentElement;
    NSMutableString *currentName, *currentTitle, *currentDated, *currentBodyText;
}

- (UITableViewCell *) getCellContentView:(NSString *)MyIdentifier;

@end

代碼的象征

//
//  NewsTableViewController.m
//  

#import "NewsTableViewController.h"
#import "NewsViewController.h"

@interface NewsTableViewController ()

@end

@implementation NewsTableViewController


dispatch_queue_t myQueue;

-(void) showHUD{

    MBProgressHUD *HUD;

    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];

    //HUD.delegate = self;
    HUD.labelText = @"News Loading";
    HUD.detailsLabelText = @"please wait...";
    HUD.square = YES;
    HUD.dimBackground = YES;

    [HUD showWhileExecuting:@selector(parserStart) onTarget:self withObject:nil animated:YES];
    //dispatch_async(dispatch_get_main_queue(), ^ {[self parserStart]; });   

}

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //NSLog(@"View Did Appear");


    myQueue = dispatch_queue_create("com.xxxxxxxxxxx.xxxxxxxxxx",NULL);
    dispatch_async(dispatch_get_main_queue(), ^ {[self showHUD]; });

}


- (void) parserStart {
    //Insert a small delay for testing purposes
    //[NSThread sleepForTimeInterval:2];

    if ([stories count] == 0) {


        NSString *path = @"http://xxx.xxxxxxxxxxx.xxx/xxxxxx/xxxxxxx.xml";
        [self parseXMLFileAtURL:path];
        //[path release];
    }
    cellSize = CGSizeMake([newsTable bounds].size.width, 60);


    [self.tableView reloadData];


}


- (void)parseXMLFileAtURL:(NSString *)URL {

    if (stories) {
        //[stories release];
        stories = nil;
    }

    stories = [[NSMutableArray alloc] init];

    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];
}


- (void)parserDidStartDocument:(NSXMLParser *)parser {
    //NSLog(@"found file and started parsing");
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSString * errorString = [NSString stringWithFormat:@"Unable to download the news feed from web site (Error code %i )", [parseError code]];
    //NSLog(@"error parsing XML: %@", errorString);

    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    //NSLog(@"found this element: %@", elementName);

    //if (currentElement) {
        //[currentElement release]; 
        //currentElement = nil; 
    //}

    currentElement = [elementName copy];

    if ([elementName isEqualToString:@"article"]) {
        // clear out our story item caches...
        item = [[NSMutableDictionary alloc] init];
        currentName = [[NSMutableString alloc] init];
        currentTitle = [[NSMutableString alloc] init];
        currentDated = [[NSMutableString alloc] init];
        currentBodyText = [[NSMutableString alloc] init];
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    //NSLog(@"found characters: %@", string);
    // save the characters for the current item...
    if ([currentElement isEqualToString:@"article"]) {
        [currentName appendString:string];
    } else if ([currentElement isEqualToString:@"title"]) {
        [currentTitle appendString:string];
    } else if ([currentElement isEqualToString:@"dated"]) {
        [currentDated appendString:string];
    } else if ([currentElement isEqualToString:@"bodytext"]) {
        [currentBodyText appendString:string];
    }
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    //NSLog(@"ended element: %@", elementName);
    if ([elementName isEqualToString:@"article"]) {
        // save values to an item, then store that item into the array...
        [item setObject:currentName forKey:@"article"];
        [item setObject:currentTitle forKey:@"title"];
        [item setObject:currentDated forKey:@"dated"];
        [item setObject:currentBodyText forKey:@"bodytext"];

        [stories addObject:[item copy]];
        //NSLog(@"adding story: %@", currentName);
    }
}


- (void)parserDidEndDocument:(NSXMLParser *)parser {

}


- (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.
}

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

}



#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [stories count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];


    if (cell == nil)

        cell = [self getCellContentView:MyIdentifier];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UILabel *lblTitle = (UILabel *)[cell viewWithTag:101];
    UILabel *lblDate = (UILabel *)[cell viewWithTag:102];
    UILabel *lblBodyText = (UILabel *)[cell viewWithTag:103];

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

    //NSString *articleValue = [[stories objectAtIndex: storyIndex] objectForKey: @"article"];
    NSString *titleValue = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
    NSString *datedValue = [[stories objectAtIndex: storyIndex] objectForKey: @"dated"];
    NSString *bodytextValue = [[stories objectAtIndex: storyIndex] objectForKey: @"bodytext"];

    lblTitle.text = titleValue;
    lblDate.text = datedValue;
    lblBodyText.text = bodytextValue;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"NewsSegue"]) {

        // note that "sender" will be the tableView cell that was selected
        UITableViewCell *cell = (UITableViewCell*)sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

        NewsViewController *nvc = [segue destinationViewController];

        int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

        nvc.title = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
        nvc.textView.text = [[stories objectAtIndex: storyIndex] objectForKey: @"bodytext"];
        //nvc.textView.text = [self getDataToPass:storyIndex.row];


        // hide the tabBar Controller
        nvc.hidesBottomBarWhenPushed = YES;

        //NSLog(@"Article : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"article"]);
        NSLog(@"Title : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"title"]);
        NSLog(@"Dated : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"dated"]);
        NSLog(@"BodyText : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"bodytext"]);    }
}

- (void)dealloc {
}

@end

現在我要推向的觀點...

//
//  NewsViewController.h
//

#import <UIKit/UIKit.h>

@class NewsViewController;

@interface NewsViewController : UIViewController {

    IBOutlet UITextView* textView;
}

@property (nonatomic, retain) IBOutlet UITextView* textView;

@end

然后是該視圖的實現文件。

//
//  NewsViewController.m
//

#import "NewsViewController.h"
#import "NewsTableViewController.h"

@interface NewsViewController ()

@end

@implementation NewsViewController

@synthesize textView;

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

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

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

在此處輸入圖片說明

編輯:據我了解,Segue的這一部分是我從解析器附帶的代碼的發送部分中發送信息的地方:

    nvc.title = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
    nvc.textView.text = [[stories objectAtIndex:storyIndex] objectForKey: @"bodytext"];

如果我將正文設置為標題,信息就會顯示出來,因此為什么我認為textview有一些不正確的地方,這就是我被困住的地方嗎?

任何幫助將不勝感激,因為我確實不知道出什么問題了! 我實際上希望我錯過的一切非常明顯! 感謝您的光臨。

我將以下內容添加到我的准備中

[nvc setTextFieldContentText:[[stories objectAtIndex:storyIndex] objectForKey: @"bodytext"]];

然后在View中加載了接收視圖

[textView setText:[self textFieldContentText]];

並且顯然在接收視圖頭文件中設置屬性

@property NSString* textFieldContentText;

感謝所有花時間尋找和幫助的人。

需要設置textView的text屬性。 嘗試這個:

self.textView.text = self.title;

我有點困惑這行代碼想要完成的事情: self.textView = self.title;

但是無論如何,我認為您的問題是您正在嘗試為尚不存在的視圖設置文本。 嘗試在新聞視圖控制器中創建一個NSString (例如textViewString ),並在prepareForSegue設置,然后在viewDidLoadviewWillAppear進行self.textView.text = self.textViewString;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM