简体   繁体   English

将数据发送到detailViewController无法正常工作?

[英]Sending data to detailViewController not working?

I have a UITableview that I have populated with data from a database, I know what to send data to another view (the detailViewController), however I can't get it to work, right now I am sending data via the - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { method. 我有一个UITableview,该数据库中已填充了数据库中的数据,我知道该如何将数据发送到另一个视图(detailViewController),但是我无法使其正常工作,现在我正在通过- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {发送数据- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {方法。 I have two classes the UITableview class and the detail class they are below. 我在下面有两个类UITableview类和detail类。 I also have an NSObject class that holds the objects for the database. 我还有一个NSObject类,用于保存数据库的对象。 Please check it out, here's my code: 请检查一下,这是我的代码:

UITableView Class code.: UITableView类代码。:

Header File

 #import <UIKit/UIKit.h>
 #import <sqlite3.h>

@interface ExerciseViewController : UITableViewController {
NSMutableArray *theauthors;
sqlite3 * db;

}
@property(nonatomic,retain) NSMutableArray *theauthors;

-(NSMutableArray *) authorList;

@end

Implementation File 实施文件

#import "ExerciseViewController.h"
#import "sqlColumns.h"
#import <sqlite3.h>
#import "UIColor+FlatUI.h"
#import "ExerciseDetailViewController.h"

@interface ExerciseViewController ()
@end
@implementation ExerciseViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.title = @"Abdominal";

[self authorList];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [self.theauthors count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"exerciseCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


sqlColumns *author = [self.theauthors objectAtIndex:indexPath.row];

UILabel *exerciseName = (UILabel *)[cell viewWithTag:101];
exerciseName.text = author.Name;

UILabel *equipment = (UILabel *)[cell viewWithTag:102];
equipment.text = author.Equipment;
NSString *string = author.Equipment;
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                           [NSCharacterSet whitespaceCharacterSet]];
if ([trimmedString isEqualToString:@"null"]) {
     equipment.text = @"No Equipment";
}

UILabel *difficulty = (UILabel *)[cell viewWithTag:103];
difficulty.text = author.Difficulty;

if ([difficulty.text isEqualToString:@"Easy"]) {
    difficulty.textColor = [UIColor emerlandColor];
}
if ([difficulty.text isEqualToString:@"Intermediate"]) {
    difficulty.textColor = [UIColor belizeHoleColor];
}
if ([difficulty.text isEqualToString:@"Hard"]) {
    difficulty.textColor = [UIColor alizarinColor];
}
if ([difficulty.text isEqualToString:@"Very Hard"]) {
    difficulty.textColor = [UIColor alizarinColor];
}

UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:100];
cellImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",author.File]];

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor cloudsColor];
bgColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bgColorView];



return cell;
}

-(NSMutableArray *) authorList{
_theauthors = [[NSMutableArray alloc] initWithCapacity:10];
@try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"StayhealthyExercises.sqlite"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

    }
    const char *query = "SELECT * FROM strengthexercises WHERE primarymuscle LIKE '%abdominal%'";
    const char *sql = query;

    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }else{

        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            sqlColumns * author = [[sqlColumns alloc] init];
            author.Name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
            author.Muscle = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
            author.Description = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
            author.File= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 4)];
            author.Sets= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 5)];
            author.Reps= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 6)];
            author.Equipment= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 7)];
            author.PrimaryMuscle= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 8)];
            author.SecondaryMuscle= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 9)];
            author.Difficulty= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 10)];

            [_theauthors addObject:author];
        }
    }
    sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
    sqlite3_close(db);

    return _theauthors;
}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"detail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    sqlColumns *author = [self.theauthors objectAtIndex:indexPath.row];
    ExerciseDetailViewController *destViewController = segue.destinationViewController;
    destViewController.exerciseImage.image = [UIImage imageNamed:author.File];
    destViewController.descriptionLabel.text = author.Description;

        }

 }


 @end

Now the DetailViewController 现在,DetailViewController

Header file: 头文件:

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

@interface ExerciseDetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *exerciseImage;
@property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
@property (nonatomic, strong) sqlColumns *author;

@end

Implementation File 实施文件

#import "ExerciseDetailViewController.h"
#import "ExerciseViewController.h"

@interface ExerciseDetailViewController ()

@end

@implementation ExerciseDetailViewController
@synthesize exerciseImage,descriptionLabel,author;

- (void)viewDidLoad
{
[super viewDidLoad];

descriptionLabel.text = author.Description;
NSLog(@"%@",author.Description);
}


@end

Must be a small error, any help would be greatly appreciated! 必须是一个小错误,任何帮助将不胜感激!

I think the problem is that your outlets are nil. 我认为问题在于您的门店数量为零。 You can only set the text and the image after the viewDidLoad get called. 您只能在调用viewDidLoad之后设置文本和图像。

Try to save your info in other properties and after viewDidLoad get called assign this info to your label and image view. 尝试将您的信息保存在其他属性中,并在调用viewDidLoad之后,将此信息分配给标签和图像视图。

In your prepare for segue: 在准备进行测试时:

destViewController.image = [UIImage imageNamed:author.File];
destViewController.text = author.Description;

In your header add those properties: 在标题中添加以下属性:

@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) NSString *text;

Then in your viewDidLoad: 然后在您的viewDidLoad中:

exerciseImage.image = self.image;
descriptionLabel.text = self.text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;willRotateToInterfaceOrientation&#39;不能用于我的DetailViewController - 'willRotateToInterfaceOrientation' not working for my DetailViewController 来自DetailViewController的数据未显示 - Data from DetailViewController not displaying 在DetailViewController Swift中更改数据 - Change data in DetailViewController Swift 当数据作为JSON传入时,将数据从tableViewController保存并发送到detailViewController的正确方法是什么? - What's the proper way of saving and sending data from tableViewController to detailViewController when data are coming as JSON 从UITableViewCell到DetailViewController的选择不起作用 - segue from UITableViewCell to DetailViewController not working 将数据从tableviewcontroller发送到detailviewcontroller - send data from tableviewcontroller to detailviewcontroller DetailViewController不显示Twitter趋势的数据 - DetailViewController not showing the data for Twitter Trends 使用Hpple解析无法在detailviewcontroller上获取数据 - Can't get data on detailviewcontroller with Hpple parsing 将数据从detailViewController向后传递到masterViewController - Pass data backward from detailViewController to masterViewController JSON数据在detailViewController中返回空单元格? - JSON data is returning empty cell in detailViewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM