无法获得披露按钮/ segue工作

时间:2013-12-03 09:49:33

标签: ios objective-c segue disclosure

我的segue似乎无法正常工作,也不会弹出披露按钮:

MADViewController.h

#import <UIKit/UIKit.h>

@interface MADViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@end

MADViewController.m

#import "MADViewController.h"
#import "PokeDetailViewController.h"
#import "Pokemonobj.h"


@interface MADViewController ()

@end

@implementation MADViewController {
    NSArray *pokemons;
}

@synthesize tableView = _tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data
    //   recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];

    Pokemonobj *poke1 = [Pokemonobj new];
    poke1.name = @"Egg Benedict";
    poke1.prepTime = @"30 min";
    poke1.imageFile = @"egg_benedict.jpg";
   poke1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs", @"4 rashers of back bacon", @"2 egg yolks", @"1 tbsp of lemon juice", @"125 g of butter", @"salt and pepper", nil];

    Pokemonobj *poke2 = [Pokemonobj new];
    poke2.name = @"Mushroom Risotto";
    poke2.prepTime = @"30 min";
    poke2.imageFile = @"mushroom_risotto.jpg";
    poke2.ingredients = [NSArray arrayWithObjects:@"1 tbsp dried porcini mushrooms", @"2 tbsp olive oil", @"1 onion, chopped", @"2 garlic cloves", @"350g/12oz arborio rice", @"1.2 litres/2 pints hot vegetable stock", @"salt and pepper", @"25g/1oz butter", nil];

    Pokemonobj *poke3 = [Pokemonobj new];
    poke3.name = @"Full Breakfast";
    poke3.prepTime = @"20 min";
   poke3.imageFile = @"full_breakfast.jpg";
    poke3.ingredients = [NSArray arrayWithObjects:@"2 sausages", @"100 grams of mushrooms", @"2 rashers of bacon", @"2 eggs", @"150 grams of baked beans", @"Vegetable oil", nil];

    pokemons = [NSArray arrayWithObjects:poke1, poke2, poke3, nil];
}

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

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    Pokemonobj *pokemon = [pokemons objectAtIndex:indexPath.row];
   cell.textLabel.text = pokemon.name;

    return cell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showPokeDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
      PokeDetailViewController *destViewController = segue.destinationViewController;
        destViewController.pokemon = [pokemons objectAtIndex:indexPath.row];

        // Hide bottom tab bar in the detail view
        //   destViewController.hidesBottomBarWhenPushed = YES;
    }
}


@end

PokeDetailViewController.h

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

@interface PokeDetailViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *recipePhoto;
@property (weak, nonatomic) IBOutlet UILabel *prepTimeLabel;
@property (weak, nonatomic) IBOutlet UITextView *ingredientTextView;

@property (nonatomic, strong) Pokemonobj *pokemon;

@end

PokeDetailViewController.m

#import "PokeDetailViewController.h"

@interface PokeDetailViewController ()

@end

@implementation PokeDetailViewController

@synthesize recipePhoto;
@synthesize prepTimeLabel;
@synthesize ingredientTextView;
@synthesize pokemon;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = pokemon.name;
    self.prepTimeLabel.text = pokemon.prepTime;
    self.recipePhoto.image = [UIImage imageNamed:pokemon.imageFile];

    NSMutableString *ingredientText = [NSMutableString string];
    for (NSString* ingredient in pokemon.ingredients) {
        [ingredientText appendFormat:@"%@\n", ingredient];
    }
    self.ingredientTextView.text = ingredientText;

}

- (void)viewDidUnload
{
    [self setRecipePhoto:nil];
    [self setPrepTimeLabel:nil];
    [self setIngredientTextView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end

Pokemonobj.h

#import <Foundation/Foundation.h>

@interface Pokemonobj : NSObject

@property (nonatomic, strong) NSString *name; // name of recipe
@property (nonatomic, strong) NSString *prepTime; // preparation time
@property (nonatomic, strong) NSString *imageFile; // image filename of recipe
@property (nonatomic, strong) NSArray *ingredients; // ingredients

@end

Pokemonobj.m

#import "Pokemonobj.h"

@implementation Pokemonobj

@synthesize name;
@synthesize prepTime;
@synthesize imageFile;
@synthesize ingredients;

@end

GameViewController.h

#import <UIKit/UIKit.h>

@interface GameViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIWebView *webView;

@end

GameViewController.m

#import "GameViewController.h"

@interface GameViewController ()

@end

@implementation GameViewController

@synthesize webView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Add code to load web content in UIWebView
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"about.html" ofType:nil]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

我不确定您是否需要查看其他代码区域。但这是现在的片段。

2 个答案:

答案 0 :(得分:0)

您确定在故事板中设置从单元格到PokeDetailViewController的连接,并将segue标识符设置为PokeDetailViewController(@“showPokeDetail”)吗?如果没有添加:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"showPokeDetail" sender:nil];
}

//仍然记得从ViewController(不是从单元格)到PokeDetailViewController(@“showPokeDetail”)设置segue标识符,

答案 1 :(得分:0)

您需要设置cell.accessoryType

尝试

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    Pokemonobj *pokemon = [pokemons objectAtIndex:indexPath.row];
   cell.textLabel.text = pokemon.name;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    return cell;
}

之后你可以实现

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

在故事板中设置带@"showPokeDetail"标识符的segue。并在上面的实现中调用segue。

[self performSegueWithIdentifier:@"showPokeDetail" sender:nil];

注意:当您使用accessoryType时,您将无法获得NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];您需要将所选索引存储在变量中。