簡體   English   中英

iOS:如何將表視圖單元格的第一個視圖控制器標簽數據傳遞給第二個視圖控制器的textView?

[英]iOS: How to pass first view controller label data of table view cell to textView of second view controller?

我正在開發一個應用程序,在其中單擊按鈕時,我想向第二個視圖控制器的textView顯示第一個視圖控制器的表格視圖單元格標簽數據。 我已經實現了代碼,但是當我單擊按鈕時,第二個視圖的textView變為空白。 請建議我在下面的代碼中我在哪里做錯了,我嘗試了很多次但未能成功。

第一視圖控制器

第二視圖控制器

第一視圖控制器:

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

    //Slide Navigation
    [self.sideNavigation addTarget:[SlideNavigationController sharedInstance] action:@selector(toggleLeftMenu) forControlEvents:UIControlEventTouchUpInside];

    WebManager *manager = [WebManager sharedInstance];
    [manager getJobPostedWithCompletionBlock:^(id response){
        NSDictionary *dictionary = (NSDictionary *)response;

        // Read data from JSON
        NSDictionary *responseObject = [dictionary objectForKey:@"response"];
        NSLog(@"The Array%@",responseObject);

        self.bids = [responseObject objectForKey:@"bids"];
        self.job_description = [responseObject objectForKey:@"job_description"];
        self.job_id = [responseObject objectForKey:@"job_id"];
        self.job_completed = [responseObject objectForKey:@"job_completed"];
        self.job_latitude = [responseObject objectForKey:@"job_latitude"];
        self.job_longitude = [responseObject objectForKey:@"job_longitude"];
        self.job_priority = [responseObject objectForKey:@"job_priority"];
        self.job_start_date = [responseObject objectForKey:@"job_start_date"];
        self.job_title = [responseObject objectForKey:@"job_title"];

        [self.jobPostedTableView reloadData];
    }];

    self.reversedGeocodes = [NSMutableDictionary dictionary];

        }

   #pragma mark - TableView Data Source

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [self.job_title count];
    }

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

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"JobTableViewCell";
        JobTableViewCell *cell = (JobTableViewCell *)[self.jobPostedTableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if(cell == nil) {

            cell = [[JobTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        // Comparing array string to display an urgent image
        if ([[self.job_priority objectAtIndex:indexPath.row]
             isEqualToString:@"urgent"]) {
            cell.urgentLabel.hidden = NO;
        } else {
                cell.urgentLabel.hidden = YES;
        }
        // Condition whether job completed is open or closed
        if ([[self.job_completed objectAtIndex:indexPath.row]
             isEqualToString:@"1"]) {
            cell.jobStatus.text = @"Open";
            [cell.jobStatus setTextColor:[UIColor colorWithRed:(84/255.f) green:(56/255.f) blue:(255/255.f) alpha:1.0f]];
            cell.flagImage.image = [UIImage imageNamed:@"jobPosted_opened.PNG"];
        } else {
            cell.jobStatus.text = @"Closed";
            [cell.jobStatus setTextColor:[UIColor colorWithRed:(179/255.f) green:(179/255.f) blue:(180/255.f) alpha:1.0f]];
            cell.flagImage.image = [UIImage imageNamed:@"jobPosted_closed.PNG"];
        }

        cell.jobTitle.text = [self.job_title objectAtIndex:indexPath.row];
        cell.jobContent.text = [self.job_description objectAtIndex:indexPath.row];
        cell.bidsLabel.text = [[self.bids objectAtIndex:indexPath.row ]stringValue];
        // Latitude and Longitude

        float lat = [self.job_latitude[indexPath.row] floatValue];
        float lng = [self.job_longitude[indexPath.row] floatValue];

        [self locationNameWithLat:lat
                              lng:lng
                completionHandler:^(NSString *locationName, NSError *error) {
                    cell.locationJob.text = locationName;
                }];


        return cell;

    }

- (IBAction)onClickJobButton:(id)sender {

    JobDetailsViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"JobDetailsViewController"];
    [self.navigationController pushViewController:vc animated:YES];
    NSIndexPath *indexPath = [self.jobPostedTableView indexPathForSelectedRow];
    JobDetailsViewController *jobDetailsController = [[JobDetailsViewController alloc]init];
    jobDetailsController.jobDetailView = [self.job_description objectAtIndex:indexPath.row];
}

第二視圖控制器:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
WebManager *manager = [WebManager sharedInstance];
NSString *user_name = manager.completeName;
self.nameLabel.text = user_name;
self.jobtextView.text = self.jobDetailView;

}

如果使用的是segues,則可以使用prepareForSegue方法發送文本:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourSecondViewController *vc = [segue destinationViewController];

        // Pass labelData which is a string
        [vc setTextString:labelData];
    }
}

並且不要忘記在SecondViewController.h聲明字符串屬性

   @property(nonatomic,strong) NSString *textString;

暫無
暫無

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

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