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

时间:2013-04-07 11:54:28

标签: ios objective-c uitableview uiscrollview presentmodalviewcontroller

我创建了一个程序,在tableview中显示来自url的书列表(任何书都有很多图像) 我想什么时候点击任何单元格进入下一页(下一页是显示图像的UIScroll)并显示该书的图像 我有一个问题,就是它何时点击任何单元格,当下一页显示黑屏而不是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

3 个答案:

答案 0 :(得分:2)

我刚刚解决了这个问题,如果你使用的是StoryBoard,你必须以这种方式初始化视图控制器:

UIStoryboard *storybrd = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

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

您必须将您的标识符放在故事板中,选择要显示的视图,然后选择身份检查器,您可以选择编写所需的ID。

答案 1 :(得分:1)

您需要致电: [[DetailViewController alloc]initWithNibName:@"nibName" bundle:nil];而不仅仅是[[DetailViewController alloc]init];,此时它没有要充气的Xib(除非你覆盖了init方法)

答案 2 :(得分:0)

你根本没有调用DetailViewController如何显示图像。在didSelectRowAtIndexPath中尝试这个:可以帮到你

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