2014-02-14 34 views
-2

我在视图中有一个NSMutableArray,我将一些JSON数据解析为表视图。我也有底部的三个按钮:按钮1,按钮2,按钮3.如果我选择按钮2,JSON数据字符串更改为链接2等...传递哪个按钮被按到另一个视图JSON

现在,当我点击一个单元格时,值(文本)传递给第二个视图控制器中的UITextView。但我想要的是也传递按下哪个按钮,所以UITextView的字符串名称解析正确。因为现在,当我选择例如按钮2并单击一个单元格时,第二个视图控制器上的文本视图变为空白,因为我只将字符串名称设置为@“message”,并且选择了文本时的字符串名称按钮2是@“鸣叫”。

这里是我的代码:

@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.com/json.php" forState:UIControlStateDisabled]; 
    [btnTwitter setTitle:@"link-2.com/json.php" forState:UIControlStateDisabled]; 
    [btnTwitter2 setTitle:@"link-3.com/json.php" 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]; 

    // Setting Up Activity Indicator View 
    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]; 

    // Initializing Data Source 
    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]; 
} 

//This is where I change the data source when selecting a button 
- (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; 
} 

//Here I am passing the selected cell data to (PostsNextView). I want to pass the string values of button 1, button 2 and button 3. How? 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row]; 
    PostsNextView *newView = [[PostsNextView alloc] init]; 
    newView.theMovie = movie; 
    [self.navigationController pushViewController:newView animated:YES]; 
} 

- (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 [email protected]"tweet":@"message"; 
} 

- (NSString *)getPostedTime 
{ 
    return [email protected]"posted":@"published"; 
} 

- (NSString *)getTwitterName 
{ 
    return [email protected]"user":@"celebname"; 
} 

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

正如你在第一种观点看,我有我解析3个不同的链接。如何在PostsNextView的viewDidLoad中设置if语句?比如,如果选择了btnFacebook:textView.text = [theMovie objectForKey:@“message”];如果选择了btnTwitter:textView.text = [theMovie objectForKey:@“tweet”]; ?

真的很感谢解决方案!

+0

[视图控制器之间传递数据](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – CoolMonster

回答

0

修订答(这将是高电平):

  • 添加标签值到每个按钮的
  • 当按钮被点击就可以得到它的标签与getTag(也标记可以设置与setTag)调用 - 请参考API

当你在编程方式加载你的下一个视图控制器,当你创建

PostsNextView *newView = [[PostsNextView alloc] init]; 
newView.theMovie = movie; 
// You could add a call here to set the value 
[newView setTheVaue:valueIWantToSet] 
  • 你会作出一个名为TheValue财产在你的NewView的控制器

当你真正使您的通话切换到新的视图控制器

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

你的愿望就已经是数据值组。

+0

嗯,我不明白。我没有使用故事板btw .. @Jeef – tracifycray

+0

我想我也没有! :) – Jeef

+0

我会考虑添加一个“标签”的按钮。这样在你的btnFromTabBarClicked中,你可以检测标签并确定哪个按钮被点击了...... – Jeef

相关问题