Hi I create button programmatically and I connect my button to another view, but I got segue problem
that I should use prepareForSegue method for storyboard but I don't know how there ar esome sample to the internet but I will get error when I used that sample, would you please help me
Thanks in advance!
here is my code
Creating Button
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
button.tag = currentTag;
currentTag++;
[button.layer setBorderColor: [[UIColor blackColor] CGColor]];
[button.layer setBorderWidth: 1.0];
[button setTitle:[NSString stringWithFormat:#"%d",currentTag] forState:UIControlStateNormal];
button.frame = CGRectMake(80*x, 32*y, 80, 32);
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
Action for button
-(void)buttonPressed:(UIButton *)button
{
NSLog(#"button %u -- frame: %#", button.tag, NSStringFromCGRect(button.frame));
WeekView *crtObj=[[WeekView alloc]initWithNibName:#"WeekView" bundle:nil];
[self.navigationController pushViewController:crtObj animated:YES];
[crtObj release];
}
Prepare for segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"WeekView"]) {
// Get reference to the destination view controller
WeekView *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object]; //// I don't know what should I write instead of object
}}
error
Edit 2:**** I have a warning :
Segues initiated directly from view controllers must have an identifier for use with -[UIViewController performSegueWithIdentifier:sender:]
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"WeekView"]) {
// Get reference to the destination view controller
// WeekView *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
// [vc setMyObjectHere:object];
[segue.destinationViewController setTitle:#"WeekView"];
}}
There are really two ways to push a view controller onto the navigation controller.
The old way, all in code. That is what you did. You can completely forget about segues at this point.
The new way: establish a Segue in Storyboard (push segue from one view controller to a new view controller, which you also define in Storyboard), give it an Identifier name and then use this code to push the view controller.
.
[self performSegueWithIdentifier:myIdentifyer sender:self];
where self is the view controller. Now you can do the customizations in prepareForSegue:.
the sample code in this case is just telling you the type of thing you can do in prepareForSegue:sender: .
it is suggesting that the viewController to which you are attempting to segue would have a method setMyObjectHere: and it would be expecting you to pass that object.
as an example, your class might have a setTitle: method, and in the prepareForSegue:sender: code, you could pre-set the title based on information in the controller you are performing the segue from.
to get rid of the warning, you need an identifier, as in the picture below:
"The old way to code", no the better way to code hopefully this helps, but if your using UIButtons and UIViews programmatically you shouldn't even be using segues.
Create a root view control in app delegate, than just pop on and off the next viewcontrollers and such.
For example this is all you need in your viewcontrollers:
-(void)nextControllerButton:(UIButton*)button{
SearchViewController *nextController = [[SearchViewController alloc] init];
nextController.username = _username.text;
nextController.password = _password.text;
[self.navigationController showViewController:nextController sender:self];
}
Related
I am having a problem, I want to change to another viewcontroller when a timer expires and this works with this code:
- (IBAction)Akkoord:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"Innameformulier"];
[self presentViewController:vc animated:YES completion:nil];
[self performseguewithidentifier:#"nextcontroller"]
}
But when I use this, my variables in my prepareforsegue method are not passing. How can I do this? I already tried [self performseguewithidentifier] but this is not working.
my preparforsegue code is:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
Innameformulier *IForm = [segue destinationViewController];
IForm.SignatureTransport = _drawImage.image;
IForm.KlantnaamInname = _Klantnaamtransport;
}
How can I call this function to happen on the timer?
In Interface Builder, select the View Controller from which you want to fire a segue, CTRL-drag from it to the destination View Controller and create a segue.
Select the segue, open the Attributes Inspector and create an Identifier for it.
Go back to your View Controller Class that fires the segue and if you don't already have it commented, add the method prepareForSegue:
In your prepareForSegue: method you should have something like the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue {
if ([segue.identifier isEqualToString:#"XXXX"]) {
DestinationVC *dVC = segue.destinationViewController;
dVC.attribute1 = self.anAttribute;
dVC.attribute2 = self.anotherAttribute;
}
}
And now you can fire the segue by calling [self performSegueWithIdentifier:#"XXXX"].
Note: you can also fire an action segue from an UIControl subclass like UIButton
OK, what you are doing here is bypassing the segue completely. You need to do something like this...
- (IBAction)Akkoord:(id)sender {
[self performseguewithidentifier:#"nextcontroller"]
}
This is all you need to do. The segue will then create the nextController for you and then the code inside prepareForSegue will pass the variables across.
If you want it on a timer then you just need to set up a timer and when it expires you can run...
[self performseguewithidentifier:#"nextcontroller"]
As long as you have a segue with this identifier then it will work anywhere. Ever after a timer.
I am new to OS X application. In iOS there is methods like :
1. self.window.rootViewController = self.viewController;
2. [self.window addSubview:self.viewController.view];
to add view controller in window or
3. DefaultViewController *objDefault = [[DefaultViewController alloc] initWithNibName:#"DefaultViewController" bundle:nil];
[self.navigationController pushViewController:objDefault animated:TRUE];
4. DefaultViewController *objDefault = [[DefaultViewController alloc] initWithNibName:#"DefaultViewController" bundle:nil];
[self presentViewController: objDefault animated:TRUE completion:nil];
to push on next view controller.
My question is that in OS X is there any method like above to add new view controller to window or push on next view controller..?
Cocoa and Cocoa-touch have a little different ways to change views on the window.
To change views you need to programmatically remove old subview and add new one.
- (void)addNewSubview:(NSView *)view // NSWindowController subclass implementation file
{
[_subview removeFromSuperview];
NSView *contentView = (NSView *)self.contentView;
[contentView addSubview:view];
_subview = view;
}
or simple [contentView replaceSubview:_subview with:view];
Since OS X 10.10 there's an opportunity to use storyboards something like in iOS.
UPD
To awake window from the application delegate class create your NSWindowController-subclass instance and show the window (it's mostly like in iOS' before-storyboards era).
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CustomWindowController *controller = [[CustomWindowController alloc] initWithWindowNibName:#"CustomWindowController"];
[controller showWindow:nil];
[controller.window makeKeyAndOrderFront:nil];
}
If you are using storyboards and segues to show a new NSViewController, then you might need to close the old view controller. Here is how I do it:
- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender
{
[self.view.window close];
}
Each view controller itself creates a window, so you need to close the old one before showing the new one.
Here is my problem (See illustration bellow):
I have a main screen (MainViewController) from which i present a form modal (ModalController) embedded in a Navigation controller.
After completing the form I want to present the result to the user and dismiss the modal.
The result is shown with a ItemViewController and it should be pushed on the main stack, i.e. in a navigation controller and if the user presses back, he returns to the main screen.
My problem is how to dismiss the modal and push the new view controller at the same time?
What I tried:
dismiss the modal and push the new view controller in the completion block using
self.parentViewController.navigationController pushViewController:itemViewController but only the modal is dismissed.
push the view controller the same way then dismiss, also without effect.
unwind the modal to the main screen and from the unwindSegue method, instantiate and push the new view controller. Unfortunately, the code below has the same effect..
- (IBAction)unwindSegue:(UIStoryboardSegue *)segue {
ItemViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ItemViewController"];
[self.navigationController pushViewController:viewController animated:YES];
}
Might be important
In the storyboard, ItemViewController is embedded in a NavigationController since it is defined as on the picture below:
Maybe this can help:
in viewDidLoad method of ItemViewController:
UIImage *backButtonImage = [UIImage imageNamed:#"back.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 55, 45);
[backButton addTarget:self
action:#selector(goHome)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
- (void)goHome{
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:0] animated:YES];
}
Right now I have a UIButton setup in my storyboard that pushes to a Table View Controller. This is working as expected. What I am trying to do is have the UIButton load an xml file when it is pressed and still move onto the Table View Controller.
How can I do this? I already have the code the XML pieces, it's a question of where the code would go and how I would add the .h and .m files for a new ViewController subclass UIViewController. How do I link those files to the UIButton I've created in storyboard?
Drag your button connection to the new view and select custom Segue instead of Push or Modal.
Change the new custom Segue's class to "mySegueClass1" or what ever you'd like to call it.
Create a new Objective-C class with the same name as you just assigned to the custom segue.
Then inside your mySegueClass1.m file add the following code, and add what ever additional actions you want to -(void)perform
-(void)perform{
UIViewController *dst = [self destinationViewController];
UIViewController *src = [self sourceViewController];
[dst viewWillAppear:NO];
[dst viewDidAppear:NO];
[src.view addSubview:dst.view];
CGRect original = dst.view.frame;
dst.view.frame = CGRectMake(dst.view.frame.origin.x, 0-dst.view.frame.size.height, dst.view.frame.size.width, dst.view.frame.size.height);
[UIView beginAnimations:nil context:nil];
dst.view.frame = CGRectMake(original.origin.x, original.origin.y, original.size.height, original.size.width);
[UIView commitAnimations];
[self performSelector:#selector(animationDone:) withObject:dst afterDelay:0.2f];
}
- (void)animationDone:(id)vc{
UIViewController *dst = (UIViewController*)vc;
UINavigationController *nav = [[self sourceViewController] navigationController];
[nav popViewControllerAnimated:NO];
[nav pushViewController:dst animated:NO];
}
There is a method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//You can access the tableviewcontroller using segue.destinationViewController
//Maybe you want to set your model here.
}
That is called before a segue is made. Here you can do all the setup you need. You should place this method inside the controller that performs the segue, not the one receiving it. (In fact, probably xcode already put that piece of code for you).
Someone help me understand the new storyboard in Xcode 4.2?
I know how to code to load another view controller but in the storyboard mode there are differences..
I also know there are a lot of tutorials about the navigationcontrollers, but I just want to switch UIViewControllers on storyboard.
With the normal .xib files I can switch views with this code from the RootViewController..
SecondViewController *Second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Second animated:YES];
When I use it in the storyboard mode it just loads the UIAlertView on the SecondViewController.m and the screen appears to be black?
Any help would be appreciated, also attached the Xcode project...
Here is the zip..
-x- Jay Ruben
you can do this:
SecondViewController *second= [self.storyboard instantiateViewControllerWithIdentifier:#"second"];
[self presentModalViewController:second animated:YES];
Don't forget to give the second view controller an identifier like "second".
Otherwise you can connect both view controllers with a segue. Hold CTRL an drag from the first to the second view Controller. Now you can choose "push" and give the segue a name to switch the View programmatically like this:
[self performSegueWithIdentifier:#"second" sender:self];
Push Segues will only work if a navigation controller is set.
You can also switch this way:
// get the view that's currently showing
UIView *currentView = self.view;
// get the the underlying UIWindow, or the view containing the current view
UIView *theWindow = [currentView superview];
UIView *newView = aTwoViewController.view;
// remove the current view and replace with myView1
[currentView removeFromSuperview];
[theWindow addSubview:newView];
// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:#"SwitchToView2"];
Download the sample project here.