Ios 根据segue重定向到不同的ViewController
我不熟悉iOS及其开发。我有tableview和两个单元格。当我按下单元格时,我想重定向到viewController对象。我想根据segue标识符进行操作。请在下面找到我使用的代码Ios 根据segue重定向到不同的ViewController,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,我不熟悉iOS及其开发。我有tableview和两个单元格。当我按下单元格时,我想重定向到viewController对象。我想根据segue标识符进行操作。请在下面找到我使用的代码 #import "Essentialinfocontroller.h" @interface Essentialinfocontroller () { NSMutableArray * titlearray; NSMutableArray * subtit
#import "Essentialinfocontroller.h"
@interface Essentialinfocontroller ()
{
NSMutableArray * titlearray;
NSMutableArray * subtitilearray;
}
@end
@implementation Essentialinfocontroller
@synthesize info;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil\];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.info.delegate=self;
self.info.dataSource=self;
titlearray =[[NSMutableArray alloc]initWithObjects:@"Custom",@"Department Of Immigration",@"Foriegn Currency Regulations",@"Sri Lankan Embassies" ,@"Sri Lankan Visa",@"When You Are There..", nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [titlearray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellidentifier=@"cell";
UITableViewCell * cell= [info dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];
cell.textLabel.text =[titlearray objectAtIndex:indexPath.row];
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"custom"])
{
//redirected to Viewcontroller custome
}
else if([segue.identifier isEqualToString:@"depart"])
{
// redirected to viewController Department
}
}
@end][1]
在要切换到新控制器的位置使用此方法
[self performSegueWithIdentifier:@"yourSegueIdentifierName" sender:nil];
一旦遇到上述语句,此方法将自动触发prepareForSegue
方法,并且如prepareForSegue
方法中所述,您正在使用if..else检查序列标识符名称,因此应该这样做
遵循以下步骤:
didselectrowatinexpath
delegate方法检查单击了tableView中的哪一行。这是为了知道我们将为特定行使用哪个segue标识符。现在您有了selectedRow索引。所以我们进入下一步performsguewithidentifier
,就会立即调用prepareforsgue
。在这方面,正如您现在所做的,您可以检查segue标识符,并在if…else代码中使用您自己的逻辑当您按下tableView上的单元格时,您需要导航到另一个viewController?@GenieWanted Yep这是我想要的。但我想根据segue IdentifierId进行操作。您尝试DidSelectRowatinexPath:method?然后我如何根据segue重定向到viewController?请在我的故事板上方查找使用您的
didSelectRowAtIndexPath
检查单击了哪一行,然后在该功能中,您可以使用我显示的上述功能。因此,基本上,它将触发一个特定的序列,该序列具有单击任何一行的给定标识符。我已正确连接。请您进一步解释。我不明白您的意思。我假设您已为这些序列指定了标识符。现在,下一步是使用didSelectRowAtIndexPath
delegate方法检查点击了哪一行。在该方法中,您需要识别单击了哪一行,并基于此,需要使用我上面显示的performsguewithidentifier
。因此,基本上,如果点击第1行,将调用标识符为XYZ的segue,如果点击第2行,将调用标识符为ABC的segue。流程如下:检查在didSelectRowAtIndexPath
中点击了哪一行,然后对该行使用适当的performsguewithidentifier
。performsguewithidentifier之后将自动调用prepareforsgue
。