使用[self presentModalViewController: animated:]和黑屏



我创建了一个程序,在tableview中显示来自url的图书列表(任何图书都有许多图像)我想什么时候点击任何单元格去下一页(下一页是显示图像的滚动),并显示那本书的图像我有一个问题,它是什么时候点击任何单元格,当去下一页显示黑屏,而不是UIScrollView包括许多图像。

这是我的代码: RecipeViewController.h
#import "RecipeViewController.h"
@interface RecipeViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) IBOutlet UITableView *table;
@end

RecipeViewController.m

#import "RecipeViewController.h"
#import "Recipe.h"
#import "DetailViewController.h"
@implementation RecipeViewController
{
    NSMutableArray *recipes;
    NSInteger num;
}
@synthesize table;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"Recipe Book";
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
    [[self navigationItem] setBackBarButtonItem:backButton];
    NSString *numberbook = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.100/mamal/book.php?all"]];
    NSInteger numbook = [numberbook integerValue];
    for (int i = 1; i <= numbook; i++)
    {
        Recipe *si = [Recipe new];
        //NSLog(@"%d,%@",i,si);
        NSString *c = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.100/mamal/book.php?info=1&b=%d",i]]];        
        NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.100/mamal/book.php?p=1&b=%d",i]]];
        si.name = [NSString stringWithString:c];
        si.imageFile = data;
        if(!recipes){
            recipes = [NSMutableArray array];
        }
        [recipes addObject:si];
    }
    num = numbook;
    // Remove table cell separator
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    // Assign our own backgroud for the view
    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    self.tableView.backgroundColor = [UIColor clearColor];
    // Add padding to the top of the table view
    UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
    self.tableView.contentInset = inset;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return recipes.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Display recipe in the table cell
    Recipe *recipe = [recipes objectAtIndex:indexPath.row];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageWithData:recipe.imageFile];
    UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
    recipeNameLabel.text = recipe.name;
    return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *obj = [[DetailViewController alloc]init];
    int x = (indexPath.row)+1;
    NSLog(@"x : %d",x);
    obj.yourValue = [NSString stringWithFormat:@"%d",(indexPath.row)+1];
    NSLog(@"yourValue1 : %@",obj.yourValue);
    obj.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:obj animated:YES];
}
@end

DetailViewController.h

#import <UIKit/UIKit.h>
#import "Recipe.h"
#import "RecipeViewController.h"
@interface DetailViewController : UIViewController<UIScrollViewDelegate>
{
    UIScrollView *scroller;
}
@property (nonatomic,strong) IBOutlet UIScrollView *scroller;
@property (nonatomic,strong) Recipe *rec;
@property (nonatomic,strong) NSString *yourValue;
@end

DetailViewController.m

#import "DetailViewController.h"
@implementation DetailViewController
@synthesize scroller,yourValue;
- (void)viewDidLoad
{
    [super viewDidLoad];
    int m1 = [self.yourValue integerValue];
   NSLog(@"M1 : %d",m1);
    NSString *bookID = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.100/mamal/book.php?info=0&b=%d",m1]]];
    NSInteger num1 = [bookID integerValue]; 
    NSLog(@"%d",num1);
    for (int i = 1; i <= num1; i++)
    {
        NSString *s = [NSString stringWithFormat:@"http://192.168.1.100/mamal/book.php?p=%d&b=%d",i,m1];
        NSData *dat = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:s]];
        NSLog(@"%@",s);
        UIImageView *imagen = [[UIImageView alloc] initWithImage:[UIImage imageWithData:dat]];
        imagen.frame = CGRectMake((i-1)*320, 0, 320, 460);
        [scroller addSubview:imagen];
    }
    scroller.delegate = self;
    scroller.contentSize = CGSizeMake(320*num1, 460);
    scroller.pagingEnabled = YES;  
}
@end

我刚刚解决了这个问题,如果你使用StoryBoard你必须这样初始化视图控制器

UIStoryboard *storybrd = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
                storybrd = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
                YourViewController *newVC =[storybrd instantiateViewControllerWithIdentifier:@"YourVCId"];

你必须把你的标识符放在故事板中,选择你想要显示的视图然后选择标识检查器,在那里你可以选择写你想要的id

您需要调用:[[DetailViewController alloc]initWithNibName:@"nibName" bundle:nil];,而不仅仅是[[DetailViewController alloc]init];,因为在这一点上,它没有Xib来膨胀(除非您覆盖init方法)

你根本没有调用DetailViewController它如何显示图像。试试这个在didSelectRowAtIndexPath:可能会帮助你

DetailViewController *DVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:DVC animated:YES];

最新更新