使用自定义代理实现条件性segue
问题描述:
我正在使用故事板。它有一个登录VC,成功推向另一个VC。当用户输入凭证并单击登录按钮时,它会打到服务器。我已经实现了所有包含与服务器在具有委托的单独类中的逻辑。当我们得到回应时,控制权转到登录VC中实现的委托方法。在代理方法中,如果状态为成功,则只有登录VC必须被推入另一个VC。使用自定义代理实现条件性segue
在登录VC
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
Util *util = [[Util alloc]init];
util.delegate = self;
NSMutableDictionary *request = [[NSMutableDictionary alloc]init];
[request setValue:uname.text forKey:@“username”];
[request setValue:pwd.text forKey:@“password”];
[util body:request];
}
当服务器返回响应谈到在登录VC实现的委托方法
- (void)response:(NSDictionary *)response
{
//here i am going to check the status if it is success i will go to new VC else in same VC
}
在这里我无法去到另一个VC,因为我在我不应该执行使用标识符:方法。
答
你为什么不叫
[self performSegueWithIdentifier:@"customerView" sender:self];
答
看起来你很可能触发您的赛格瑞当登录按钮被窃听。如果您需要等待Web服务的响应,则无法执行此操作。相反,尝试将您的按钮连接到发送您的登录请求时的方法。当您收到您的回复,你可以检查用户的登录是有效的,然后编程执行你赛格瑞:
- (void)response:(NSDictionary *)response
{
if (something) // check if response is valid
{
[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:self];
}
else
{
// show error
}
}
这样一来,你就不会需要实现shouldPerformSegueWithIdentifier:sender:
可言。