Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.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 在视图之间传递数据并将字符串加载到文本视图_Ios_Objective C_Json_Uitableview - Fatal编程技术网

Ios 在视图之间传递数据并将字符串加载到文本视图

Ios 在视图之间传递数据并将字符串加载到文本视图,ios,objective-c,json,uitableview,Ios,Objective C,Json,Uitableview,我在一个视图中有一个NSMutableArray,其中我将一些JSON数据解析为一个表视图。现在,当我单击一个单元格时,我想将所选单元格数组的值(文本)传递给另一个视图中的UITextView 存储数据的NSMutableArray称为movies 以下是我的第一个视图代码: @interface DEMOSecondViewController () @end @implementation DEMOSecondViewController @synthesize tableView

我在一个视图中有一个
NSMutableArray
,其中我将一些JSON数据解析为一个表视图。现在,当我单击一个单元格时,我想将所选单元格数组的值(文本)传递给另一个视图中的
UITextView

存储数据的
NSMutableArray
称为movies

以下是我的第一个视图代码:

@interface DEMOSecondViewController ()  

@end

@implementation DEMOSecondViewController
@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView;
@synthesize fontForCellText;
@synthesize btnFaceBook, btnTwitter, btnTwitter2;
@synthesize strURLToLoad;
@synthesize movies;

- (IBAction)showButtonMenu {
    [self.frostedViewController presentMenuViewController];
}

- (void)refresh:(UIRefreshControl *)refreshControl {
    [refreshControl endRefreshing];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl setTintColor:[UIColor greenColor]];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:refreshControl];

    strURLToLoad = [[NSMutableString alloc] init];

    [btnFaceBook setTitle:@"link-1" forState:UIControlStateDisabled];
    [btnTwitter setTitle:@"link-2" forState:UIControlStateDisabled];
    [btnTwitter2 setTitle:@"link-3" forState:UIControlStateDisabled];

    [btnFaceBook setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
    [btnFaceBook setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected];

    [btnTwitter setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
    [btnTwitter setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected];

    [btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
    [btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected];

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PostsObject" owner:self options:nil];
    PostsObject *cell = [nib objectAtIndex:0];
    fontForCellText = cell.title.font;
    cellTextWidth = cell.title.frame.size.width;
    cellHeightExceptText = cell.frame.size.height - cell.title.frame.size.height;

    [self.navigationController setNavigationBarHidden:YES];

    self.tableView.separatorColor = [UIColor clearColor];

    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    self.activityIndicatorView.color = [UIColor greenColor];

    self.activityIndicatorView.hidesWhenStopped = YES;
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
    self.tableView.separatorColor = [UIColor clearColor];

    movies = [[NSMutableArray alloc] init];

    [self btnFromTabBarClicked:btnFaceBook];
}

- (void)loadJSONFromCurrentURL
{
    [self.activityIndicatorView startAnimating];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:strURLToLoad]];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        [movies setArray:JSON];
        [self.activityIndicatorView stopAnimating];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];
}

- (IBAction)btnFromTabBarClicked:(UIButton *)sender
{
    btnFaceBook.selected = btnTwitter.selected = btnTwitter2.selected = NO;

    sender.selected = YES;

    [strURLToLoad setString:[sender titleForState:UIControlStateDisabled]];

    [self loadJSONFromCurrentURL];
}

// Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return movies.count;

}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(indexPath.row == 0) {
        static NSString *Identifier1 = @"TableHeaderView";


        TableHeaderView *cell = [tableView dequeueReusableCellWithIdentifier:Identifier1];
        if (cell == nil) {
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"TableHeaderView" owner:self options:nil];
            cell = (TableHeaderView *)[nib objectAtIndex:0];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            cell.backgroundColor = [UIColor clearColor];
            return cell;
        }

    } else {
        static NSString *Identifier2 = @"PostsObject";

        PostsObject *cell = [tableView dequeueReusableCellWithIdentifier:Identifier2];
        if (cell == nil) {
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"PostsObject" owner:self options:nil];
            cell = (PostsObject *)[nib objectAtIndex:0];
            cell.backgroundColor = [UIColor clearColor];
        }

        NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
        NSString *strText = [movie objectForKey:[self getTextKey]];

        CGRect rect = cell.title.frame;
        rect.size.height = [self getHeightForText:strText];
        cell.title.frame = rect;
        cell.title.text = strText;
        cell.arrow.center = CGPointMake(cell.arrow.frame.origin.x, rect.origin.y + rect.size.height/2);
        cell.published.text = [movie objectForKey:[self getPostedTime]];
        cell.twitterName.text = [movie objectForKey:[self getTwitterName]];

        return cell;

    }
    return 0;
}

//When I click on the cell, I want the selected cell-text to pass to another view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //How can I do this?
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0) {
        return 224;

    } else {
        NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
        NSString *strText = [movie objectForKey:[self getTextKey]];

        CGFloat cellHeight = cellHeightExceptText + [self getHeightForText:strText];
        return cellHeight;
    }

}

- (NSString *)getTextKey
{
    return btnTwitter.selected?@"tweet":@"message";
}

- (NSString *)getPostedTime
{
    return btnTwitter.selected?@"posted":@"published";
}

- (NSString *)getTwitterName
{
    return btnTwitter2.selected?@"user":@"usernew";
}

- (CGFloat)getHeightForText:(NSString *)strText
{
    CGSize constraintSize = CGSizeMake(cellTextWidth, MAXFLOAT);
    CGSize labelSize = [strText sizeWithFont:fontForCellText constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"labelSize.height = %f",labelSize.height);
    return labelSize.height;
}

@end
到目前为止,第二个视图的代码如下所示:

@interface PostsNextView : UIViewController 

@property (nonatomic, retain) NSDictionary *theMovie;

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

@end

那么,如何从第一个视图获取要传递并显示在此文件文本视图中的值?

添加到didSelectRowAtIndexPath代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //get the data you want to pass
    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];

    // Allocate second view you want to open
    PostsNextView *newView = [[PostsNextView alloc] init];
    // pass the data 
    newView.theMovie = movie;
    // present view
    [self.navigationController pushViewController:newView animated:YES];
}
在viewDidLoad中的PostsNextView.m文件中,您可以显示要显示的文本

//延伸

添加属性以跟踪按下的按钮:

@interface PostsNextView : UIViewController 

@property (nonatomic, retain) NSDictionary *theMovie;
@property (nonatomic, retain) IBOutlet UITextView *textView;
@property  int whichButton;

@end
在视图中,是否添加此行:

newView.theMovie = movie;
newView.whichButton = _witchBtn;
在DEMOSecondViewController.m文件中添加ivar:

@implementation DEMOSecondViewController
{
    int _witchBtn;
}

在同一个文件中,如果您将其更改为Facebook assign(比如Facebook assign)witchBtn=1、如果twitter(twitter)witchBtn=2等,则将值指定给(例如0)作为默认值(所有按钮均已禁用)

。但正如您在第一个视图中看到的,我正在解析3个不同的链接。如何在PostsNextView的viewDidLoad中设置if语句?例如,如果选择了btnFacebook:textView.text=[theMovie objectForKey:@“message”];如果选择了btnTwitter:textView.text=[TheMovieObjectForkey:@“tweet”]@GregYou可以向PostsNextView.h添加新属性,例如@property int buttonype;您可以传递在didSelectRowAtIndexPath中按下的按钮:然后在viewDidLoad中使用self。按钮键来设置你的逻辑。嗯,我想不清楚。你能帮我弄一下密码吗@格雷格