无法让披露按钮/segue 工作

Posted

技术标签:

【中文标题】无法让披露按钮/segue 工作【英文标题】:can't get disclosure button/segue to work 【发布时间】:2013-12-03 09:49:32 【问题描述】:

我的 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

口袋妖怪obj.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

口袋妖怪obj.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

我不确定您是否需要查看其他代码区域。但现在是 sn-p。

【问题讨论】:

【参考方案1】:

您确定在情节提要中设置了从您的单元到 PokeDetailViewController 的连接,并且您将 segue 标识符设置为 PokeDetailViewController (@"showPokeDetail" )?如果不添加:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

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

//还记得设置segue标识符从你的ViewController(不是从cell)到PokeDetailViewController(@"showPokeDetail"),

【讨论】:

【参考方案2】:

你需要设置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]; 您需要将选定的索引存储在一个变量中。

【讨论】:

以上是关于无法让披露按钮/segue 工作的主要内容,如果未能解决你的问题,请参考以下文章

详细信息披露按钮和 Segues

从详细信息披露按钮 segue 传递对象

如何让披露按钮与 BKSplitView 一起玩得很好

Segue 动画无法完全正常工作

使用带有自定义按钮的主详细信息应用程序的 segue。

iOS:让segue等待登录成功