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.");
}
Related
I am using ECSlidingViewController in my app for Facebook style swipe effect. I have 2 TopViewControllers, one left and one right side view controller and one initviewcontroller which is a slidingviewcontroller subclass.
1. InitViewController:ECSlidingViewController
2.MainViewController (TopViewController)
3.LeftMenuViewContrller (UIViewController)
4.RightMenuViewController (UIViewController)
5.DetailViewController (TopViewController)
I have tableview on my MainViewController which contains the calendar events. The problem is that I want to go to MainViewController to DetailViewController by clicking the event in tableView:didSelectRowAtIndexPath: methods to show the events details which is another topview controller with some animation( which I am unable to get so far). I am using following code which is getting me to the DetailViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailsViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"details"];
self.slidingViewController.topViewController = newTopViewController;
[self.slidingViewController resetTopView];
}
this is my viewDidLoad method
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[LeftViewController class]]) {
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Left"];
}
if (![self.slidingViewController.underRightViewController isKindOfClass:[RightViewController class]]) {
self.slidingViewController.underRightViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Right"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
}
I want to get the animation like the UINavigationController or Modal style
[self.navigationController pushViewController:newTopViewController animated:YES];
or
[self presentViewController:newTopViewController animated:YES completion:nil];
please help. thank you.
Are you using Storyboard? Could you perform a Segue? In the storyboard setup the name and the link and style as Modal along with the Transition.
In your DidSelectRowAtIndex put this
[self performSegueWithIdentifier:#"eventDetail" sender:#(indexPath.row)];
Then declare this method
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"eventDetail"])
{
DetailsViewController *destViewController = segue.destinationViewController;
destViewController.eventRow = sender;
}
// You can test for other Segue names here
}
Then in the DetailsViewController.h put
// Allows sending View Controller to give this controller data
#property (nonatomic, strong) int *eventRow;
And in the .m put
#synthesize eventRow;
Now in your DetailsViewController it should animate to it and the sliding will be disabled (unless you declare all the ECS bits as normal). You also have an Int eventRow which tells you which cell was clicked.
You can create a back button and do this;
[self dismissViewControllerAnimated:YES completion:nil];
EDIT
OR you could try this but I am not sure if it will animate plus you need to figure out how to pass stuff, maybe a combination of the two...
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"eventDetail"];
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];
I have create two view controller onto storyboard I wanted to go from one view controller to another view controller programmatically. I have create a button in one view controller. I know it very easy to go from one view controller to another just drag line from button from oneviewcontroller to second view controller. but I want to do it programmatically because I have if and else conditions in button when if condition true then I want to move it to next view controller this is the code.
-(IBAction)loadView:(id)sender {
if([username.text isEqualToString:#"adnan"] && [password.text isEqualToString:#"bhatti123"] )
{
// here I want to write that code which reach me to second view controller which I implement on storyboard
}
else
{
}
}
Drag line from first view controller to second and set the identifier for segue:
In the .m file:
if([username.text isEqualToString:#"adnan"] && [password.text isEqualToString:#"bhatti123"] )
{
[self performSegueWithIdentifier:#"nextView" sender:sender];
}
else
{
}
-(IBAction)loadView:(id)sender
{
if([username.text isEqualToString:#"adnan"] && [password.text isEqualToString:#"bhatti123"] )
{
// here i want to write that code which reach me to second view controller which i implement on storyboard
SecondViewController *objSecondViewController = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:objSecondViewController animated:YES];
[objSecondViewController release];
}
else
{
//write your else part code here
}
}
UPDATE :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
SecondViewController *objSecondViewController = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
[self.navigationController pushViewController:objSecondViewController animated:YES];
SecondViewController *secondviewController = [[SecondViewController alloc] init];
[self presentviewController:secondviewController animation:YES completion:nil];
Hope this helps...
I'm coding in Xcode, and i have a problem moving from view to view (xib to xib) My xib-files all have a personal .h and .m file.
this is the code i have:
The code to go from the first view to the quiz view in this case:
-(IBAction)switchviewMainToQuiz:(id)sender {
ViewControllerQuiz *quizView = [[ViewControllerQuiz alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:quizView animated:YES];
}
The code to go back from a view to my first view:
-(IBAction)switchviewQuizToMain:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
the problem is that the second code just goes back and that gives sometimes an error or a fail in the app. So when i go to other views and then want to go back it often gets stuck. So does someone have an other code to switch the views, or maybe an other solution? Thank You!
I wouldn't create new viewController in IBAction. For me i would do this in this way:
1 Create property with viewController:
#property(nonatomic, strong) UIViewController *quizView ;
2 Synthesize it:
#synthesize quizView = _quizView;
3 Create method:
- (UIViewController *) quizView {
if (_quizView == nil) {
_quizView = [[ViewControllerQuiz alloc] initWithNibName:nil bundle:nil];
}
return _quizView;
}
4 In your IBAction:
-(IBAction)switchviewMainToQuiz:(id)sender {
[self presentModalViewController:self.quizView animated:YES];
}
How to pass values from one controller to another??? I use a StoryBoard.
I would like this to appear on the highlighted text view of the first view.
Call the next view of the code, I think something like this should look like:
UIStoryboard *finish = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = [finish instantiateViewControllerWithIdentifier:#"FinishController"];
viewController.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:viewController animated:YES];
finishcontroller:
- (void)viewDidLoad
{
self.lblFinishTitle.text=self.FinishTitle;
self.lblFinishDesc.text = self.FinishDesc;
self.lblFinishPoint.text=self.FinishPoint;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
first view:
-(void) prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender
{
if ([segue.identifier hasPrefix:#"FinishController"]) {
FinishController *asker = (FinishController *) segue.destinationViewController;
asker.FinishDesc = #"What do you want your label to say?";
asker.FinishTitle = #"Label text";
asker.FinishPoint = #"asdas";
}
}
I want to pass a value causing the transmission of the code
The issue is that you aren't actually using that segue, you are using presentModalController instead.
Note that usually, you can just ask self for it's storyboard. However, even that is unnecessary when you have a segues connected:
[self preformSegueWithIdentifier:#"FinishController" sender:self];
Then prepareForSegue will be called. Also note that you can (should) use something more authoritative than the segue identifier to determine if you should load the data... you can ask the segue's destination controller if it is the right class:
-(void) prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isKindOfClass:[FinishController class]]) {
FinishController *asker = (FinishController *) segue.destinationViewController;
asker.FinishDesc = #"What do you want your label to say?";
asker.FinishTitle = #"Label text";
asker.FinishPoint = #"asdas";
}
}
You are probably already aware (since you used the identifier in your code), but for the benefit of future discoverers of this post; segues are given identifiers in the inspector panel of Xcode when you are in the storyboard.
I have three buttons like this each corresponds to a different UIViewController:
For example, I press button 1. It will pass to the start button the view controller corresponding to that number that it will show.
I need to be able to pass it in this button:
Is it possible?
Yes, just create the UIButton as property on the view controller shown on button tap. Then, in the handler for the button tap, use the button property on the new controller to assign the button before showing the new controller.
Sample code:
//here's your method invoked when pressing the button 1
- (IBAction) pressedButtonOne:(id) sender {
NewViewController *newVc = [[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil];
newVc.startButton = self.startButton;
}
Here's the code in the controller created when pressing button 1
NewViewController.h
#import <UIKit/UIKit.h>
#interface NewViewController : UIViewController
#property (nonatomic, retain) UIButton *startButton;
//NewViewController.m
- (void) viewDidLoad {
//use self.startButton here, for whatever you need
}
If I understand the question correctly: Let's talk for button one (you can generalize it to the other buttons and VC's). Say the associated viewController is called button1ViewController.
In your button1ViewController, declare a property called button (#property(strong,non atomic) UIButton *button - also synthesize)..
In your IBAction method.
-(IBAction) button1Tapped:(id)sender {
button1ViewController.button = sender;
[self presentViewController:button1ViewController;
}
Edit: I think I get the question now.
In your mainView controller declare an instance variable: UIViewController *pushedController.
Also declare instances Button1ViewController *button1ViewController;
, Button2ViewController *button2ViewController, Button3ViewController *button3ViewController.
Now:
-(IBAction) button1Pressed:(id)sender {
if (button1ViewController==nil) {
button1ViewController = [Button1ViewController alloc] init];
}
pushedController = button1ViewController;
}
-(IBAction) button2Pressed:(id)sender {
if (button2ViewController==nil) {
button2ViewController = [Button2ViewController alloc] init];
}
pushedController = button2ViewController;
}
//same thing for button 3 pressed with button 3 controller
-(IBAction) startButtonPressed:(id) sender {
if (pushedViewController!=nil)
[self presentViewController:pushedViewController animated:YES completion:NULL];
}
I think this should work, however I haven't tried the code myself.