I have 3 view controllers. view1, view2, view3 View1 & view2 has add button both button called same view controller view3. how do you know which button called view 3
plz let me know how to know which button called view3
create a property and synthesize it on your view controller 3 like:
@property (nonatomic, retain) NSString *ComingForm;
Now on your button touched event on your viewcontroller #1 & #2 while you create your object of viewcontroller 3 to push just set "ComingForm" value. like
viewcontroller3 *__viewcontroller3=[[viewcontroller3 alloc] init];
[__viewcontroller3 setComingForm: @"View1"];
Now you can easly got from which viewcontroller you are coming from.
In first view .h file
@property (nonatomic)int btncTag;
In first view .m file
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"btnc"])
{
btncTag =1;
ButtonfinalViewController *btnf = segue.destinationViewController;
btnf.Tag = btncTag;
}
}
In second view .h file
@property (nonatomic)int btnpTag;
In second view .m file
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"pbtn"])
{
btnpTag = 2;
ButtonfinalViewController *btnf = segue.destinationViewController;
btnf.Tag = btnpTag;
}
}
in Third view .h
@property (nonatomic)int Tag;
in Third view .m
- (IBAction)btnAdd:(id)sender
{
ButtonCViewController *btnc = [[ButtonCViewController alloc]init];
btnc.btncTag = Tag;
ButtonPViewController *btnp = [[ButtonPViewController alloc]init];
btnp.btnpTag =Tag;
if (Tag == 1)
{
UIStoryboard *menuViewStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ButtonCViewController *btnc = [menuViewStoryboard instantiateViewControllerWithIdentifier:@"BtnCView"];
[self presentViewController:btnc animated:YES completion:nil];
}
if (Tag == 2)
{
UIStoryboard *menuViewStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ButtonPViewController *btnp = [menuViewStoryboard instantiateViewControllerWithIdentifier:@"BtnPView"];
[self presentViewController:btnp animated:YES completion:nil];
}
}
You can set tag
property of the Add Buttons from the view1 and view2 to say 1 and 2 respectively.
btnInView1.tag = 1; //in view1.
btnInView2.tag = 2; //in view2.
What you have to do is to create property for some int
variable say tagNo
in view3.
@property (nonatomic) int tagNo; //in view3.
You have to set the tagNo
to the tag
value of Add Button before changing to view3.
ViewController3 *view3 = [[ViewController3 alloc] initWithNibName:@"ViewController3" bundle:nil];
view3.tagNo = btnInView1.tag //in view1.
//view3.tagNo = btnInView2.tag //in view2.
Now, in view3, you just have to check the value of tagNo
.
If it's 1 that means view3 is loaded from view1 else from view2 or you can say if it's 1 that means view3 is loaded by Add Button inside view1 else by Add Button inside view2.
if (self.tagNo == 1)
{
NSLog("view3 is loaded by Add Button inside view1.");
}
else if (self.tagNo == 2)
{
NSLog("view3 is loaded by Add Button inside view2.");
}
else
{
NSLog("It will never reach here.");
}