Segue to ABPersonViewController in Storyboard - ios

I am trying to segue to a ABPersonViewController from a button in the Storyboard.
But if I do that the screen is completely black.
If I use a IBAction for the button and use the following code it works:
ABPersonViewController *person = [[ABPersonViewController alloc]init];
[self.navigationController pushViewController:person animated:YES];
Why is that? am I doing wrong?
EDIT: I found a work around but I don't think this is a proper way to do it. I subclassed ABPersonViewController and overrode the initWithCoder method with the following:
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [self initWithNibName:nil bundle:nil];
return self;
}

Since you are using storyboards you should should name the segue you are using and then this in your IBAction:
[self performSegueWithIdentifier:#"abPersonViewControllerSegue" sender:self];
This way you do not even need to manually call alloc/init. Then in your prepareForSegue you could set any attributes:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"abPersonViewControllerSegue"])
{
[segue.destinationViewController setAttribute:#"whatever you want"];
...
}
}
If this is not what you are looking for please let me know.

You don't set the displayedPerson property ...
#property(nonatomic, readwrite) ABRecordRef displayedPerson

Since you are using Storyboard, why do you still call [[ABPersonViewController alloc]init]?The Storyboard will handle the creation and pushing of the ABPersonViewController(if you specified Push action for the segue). As long as you control dragged from the Button to the ABPersonViewController in story board, you do not need to write a single line of code. If you control dragged from the view controller that contains the button, then you should be calling performSegue to trigger the segue(you need to set the identifier for the segue in this case) that adds the ABPersonViewController to the navigation controller.

Related

Button not responding to selector

UPDATED FOR COMMENTS
I am trying to make this code work, but whenever I press the "nextButton", the program ends and I cannot figure out what went wrong. Can you please look at my code and figure out if anything is wrong.
- (void)viewDidLoad {
[super viewDidLoad];
Player1.placeholder = #"Player 1";
Player2.placeholder = #"Player 2";
Notes1.placeholder = #"Notes";
Notes2.placeholder = #"Notes";
[nextButton setAction:#selector(nextButtonPressed:)];
}
-(IBAction)nextButtonPressed:(id)sender {
if ([self.delegateP respondsToSelector: #selector(addPlayerViewController:didFinishEnteringPlayer1:didFinishEneteringPlayer2:)]) {
[self.delegateP addPlayerViewController:self didFinishEnteringPlayer1:Player1.text didFinishEneteringPlayer2:Player2.text];
NSLog(#"Works");
}
[self performSelector:#selector(nextButtonPressed:) withObject:nil afterDelay:0.25];
}
I also have another question to do with this code. To pass information through different view controllers using delegates, do you have to use a button or a bar button item?
The error that is displaying:
-[UIStoryboardPushSegueTemplate nextButtonPressed:]: unrecognized selector sent to instance
First you can´t put both lines:
[self presentModalViewController:gameDetails animated:YES];
[self.navigationController pushViewController: gameDetails animated:YES];
Second go to your viewController PlayersViewController and click in storyboard click on editor (top menú XCode) Embed In > Navigation Controller now you with your code change the next lines:
-(void) nextButtonPressed {
NSLog(#"Hi my friend");
[self performSegueWithIdentifier:#"YOUR_SEGUE_NAME_HERE" sender:#"hi"];}
And create new function:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:sender];
}}
The ViewController "YourViewController.h :
#interface YourViewController : UIViewController
#property (nonatomic, strong) NSString *MyObjectHere;
#end
Try setting a symbolic breakpoint on all Objective-C exceptions and see if that gives you your crash line numbers and error messages again.
The code you posted for setting up the target/action on your bar button item looks correct.
Edit: Disregard this part about changing the selector.
(I'm leaving it for continuity, but ignore it.)
One thing you might try is changing the selector to
:#selector(nextButtonPressed:) (Colon added)
And then adding the button as a parameter to your action method:
-(IBAction) nextButtonPressed: (UIButton *) sender
Action methods should work with or without the sender parameter, but
it's worth trying.
(Also you should add the IBAction keyword for clarity even if you're not hooking up the action in IB.)
EDIT: rmaddy pointed out that your code is trying to both modally present and push the same view controller. Don't do that. Do one thing or the other.
You should use either
[self presentModalViewController:gameDetails animated:YES];
(To present it as a modal)
or
[self.navigationController pushViewController: gameDetails animated:YES];
(To push it onto the navigation stack.
But not both. Doing both is almost certainly the cause of your crash.

Method not working when called from another class

I am trying to call the method addObjectToArray from SecondViewController.m. The NSLog works, however I cannot add "foo" to _myArray (an NSMutableArray that is the data source for the UITableView). If I call [self addObjectToArray] in viewDidLoad, then it works fine.
FirstViewController.m
-(void)addObjectToArray {
[_myArray addObject:#"foo"];
[_myTableView reloadData];
NSLog(#"it works");
}
SecondViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"identifier"]) {
FirstViewController *controller = [segue destinationViewController];
[controller addObjectToArray];
}
}
As per Nofel Mahmood's comment, you are creating two separate instances of FirstViewController, one named firstViewController (created from the storyboard) and one called myObject (created with the 'new' method). You then call the addObjectToArray method on myObject, but you present firstViewController. Your myObject is essentially redundant. Amend your code as follows:
-(IBAction) doSomething:(id)sender {
FirstViewController *firstViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"firstViewController"];
[firstViewController addObjectToArray];
[self presentViewController:firstViewController animated:YES completion:nil];
}
EDIT
Since you want to return to an existing instance of FirstViewController, you need to use an unwind segue. There's a detailed explanation here: what-are-unwind-segues, but in your case:
In FirstViewController, add a new method:
- (IBAction)unwindToFirst:(UIStoryboardSegue *)unwindSegue {
[self addObjectToArray];
}
Then in your storyboard, ctrl-drag from the SecondViewController (or if you prefer, from a specific control in the view) to the green "Exit" icon in the bar below the SecondViewController. You should then select the unwindToFirst action in the small popup that appears.
If you want to use this unwind segue from code, look in the Document Outline on the left hand side of your storyboard for the Unwind Segue you just created. Select this, and then add an identifier in the attributes inspector on the right hand side. You can then call this segue from code using the normal [self performSequeWithIdentifier:...] method.

iOS: dismiss popOver generated with Interface Builder with button within the popOver

I have a button which leads to a popOver, all created in Interface Builder. The popOver is closed when I press somewhere outside of it, but I would also like to implement a button within the popOver which does that.
I found a solution by Giorgio Barchiesi dating back to 2011, however I fail to implement it. Here's his solution:
In the implementation file of the source view controller:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue destinationViewController] isKindOfClass:[MyDestViewController class]]) {
MyDestViewController* viewController = (MyDestViewController*)[segue destinationViewController];
UIStoryboardPopoverSegue* popoverSegue = (UIStoryboardPopoverSegue*)segue;
[viewController setPopoverController:[popoverSegue popoverController]];
}
}
In the header file of the destination view controller:
#property (weak, nonatomic) UIPopoverController* popoverController;
In the implementation file of the destination view controller:
#synthesize popoverController;
Same file, whenever you want to dismiss the popover:
[popoverController dismissPopoverAnimated:YES];
i could call the last function when the button is pressed.
My problem is that XCode gives me an error on the [viewController setPopoverController:[popoverSegue popoverController]] line: ARC Semantic Issue: No known class method for selector 'setPopOverController'
What did I miss to implement?
Here is the method I use:
Open your storyboard file, select the segue arrow and open the Attributes Inspector (Option - Command - 4) and identifier fill in a sensible name, like "myPopoverSegue".
In your Source View Controller define a variable right after #implementation :
#implementation ViewController
{
__weak UIPopoverController *myPopover;
}
Then, again in the Source VC:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:[dict objectForKey:#"myPopoverSegue"]]) {//#"segue" is your segue name. You can use isKindOfClass as you do currently, I prefer this method.
myPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
}
}
-(void)closePopover{
[myPopover dismissPopoverAnimated:YES];
}
In the end of your Source VC's viewDidLoad method write:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(closePopover) name:#"popoverShouldDismiss" object:nil];
Finally, whenever you want to dismiss the popover:
[[NSNotificationCenter defaultCenter] postNotificationName:#"popoverShouldDismiss" object:nil];
Hope this helps!
This way you will also be able to change the segue to a different controller without changing your code.
You can add the delegate < UIPopoverControllerDelegate > to your class and override the delegate method:
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
return NO;
}
This will prevent the popover to be dismissed when user presses anywhere on screen.
Now you can dismiss your popover inside the button's selector method by using:
[popoverController dismissPopoverAnimated:YES];
In iOS 8 it's really easy. Just call
[self dismissViewControllerAnimated:YES completion:^{}];
Pop overs are regular presentation controllers, so it's more or less the exact same thing as a modal view controller.
Try this code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue destinationViewController] isKindOfClass:[MyDestViewController class]])
{
MyDestViewController* viewController = (MyDestViewController*)[segue destinationViewController];
UIStoryboardPopoverSegue* popoverSegue = (UIStoryboardPopoverSegue*)segue;
popoverSegue.popoverController=[[UIPopoverController alloc] initWithContentViewController:viewController];
[popoverSegue.popoverController setPopoverContentSize:CGSizeMake(viewController.view.frame.size.width, viewController.view.frame.size.height)];
popoverSegue.popoverController.delegate=self;
[viewController setPopoverController:popoverSegue.popoverController];
}
}
I hope it helps you.

Best way to push additional UIViewControllers onto a UINavigationController

I'm working with the Master-Detail project template that comes with Xcode and referenced in http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/SecondiOSAppTutorial/
Problem: I am trying to figure out how to add additional UIViewControllers to the default UINavigationController that this template comes with.
Specifically, I would like to add a DetailEditViewController after DetailViewController. Here is what I've done to this effect so far:
In DetailViewController I added an edit button to the navigationItem:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:#selector(editDetailItem:)];
[self configureView];
}
You can see it specifies a message selector editDetailItem:, which I've implemented as:
- (void)editDetailItem:(id)sender
{
[self.navigationController pushViewController:
[[DetailEditViewController alloc] init] animated:YES];
}
I've created a DetailEditViewController on the Storyboard, and the code runs without crashing, producing a black, blank window with a navigation item to take me back to detail. From here on I am pretty confused:
When I drag a new View Controller to the Storyboard, no corresponding code files are created! Am I responsible for making code files for these controllers? I see that Storyboard View Controllers are associated with a Class in the Identity Inspector... but why on earth would it not create templates for a new UIViewController when I drag one onto the Storyboard?
Should I be using a seque instead of -pushViewController to get from DetailViewController to DetailEditViewController? If so, I'm not sure how to add one on the Storyboard, because the navigationItem's UIBarButtonItems are all added in-code. There's nothing to Ctrl-drag from.
How do I send information from DetailViewController to DetailEditViewController? When MasterViewController segues to DetailViewController, it specifies the sender via - prepareForSegue:sender:
You're right, no corresponding files are produced. How is the system supposed to know what class you want? You need to create a UIViewController subclass, and change the class of the controller you drag in, to that class. The easiest way to push the new controller is to use a push segue -- if you don't have a UI element in the storyboard to connect that to, you connect it directly from the controller and give the segue an identifier (which I call "GoToEdit" in my example). In the action method for the edit button, then perform the segue:
[self performSegueWithIdentifier:#"GoToEdit" sender:self];
If you want to pass information, then you implement prepareForSegue:, something like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"GoToEdit"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
It's a good thing to check the segue identifier first. Then you can access your destinationViewController (you might have to cast it to your class, so the compiler will recognize any property of it you're trying to set), and pass what you want to it.

UITableViewController loses scroll functionality if presented by a popover as model

Any reason why a model tableview (UITableViewController) is not scrolling a table when the keyboard appears ONLY if presented by a popover controller?
I basically have a popover menu with a button that opens a model Tableview fullscreen, text fields within rows at the bottom of the table get hidden by the keyboard when tapped.
This is not the same as a UIView or Scrollview where you have to manage scrolling manually. The UITableViewController has this functionality built in, it just doesn't work if there is a popover controller somewhere back down the hierarchy.
Sample project: www.geoffcoope.co.uk/ios/testTVCScrolling.zip
Thanks
Geoff
Either your UITableView is sending its delegate method requests to the popover's view controller instead of your UITableView controller, or your UITableView's delegate is set to nil. Look where you are instantiating your TableView and make sure you are setting the delegate property correctly.
I guess your problem is with a way a storyboard's segue shows popover controllers. It looks like it is a common problem.
So, the easiest way to solve it, is to move the creation of your modal window inside a code. Something like that:
Inside your ViewController.h write
#property (strong) UIPopoverController * popoverController;
- (void) showNavigationController;
Inside your ViewController.m write
#synthesize popoverController;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[super prepareForSegue:segue sender:sender];
if ([segue isKindOfClass:[UIStoryboardPopoverSegue class]]){
if (popoverController) [popoverController dismissPopoverAnimated:NO];
UIStoryboardPopoverSegue * popoverSegue = (UIStoryboardPopoverSegue *) segue;
popoverController = popoverSegue.popoverController;
}
}
- (void) showNavigationController
{
[popoverController dismissPopoverAnimated:NO];
popoverController = nil;
UIStoryboard *ub = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *controller = [ub instantiateViewControllerWithIdentifier:#"navigationController"];
[self presentModalViewController:controller animated:YES];
}
then inside your MainStoryboard set identifier navigationController for your navigation controller and instead of segue on your "Show Table" button set a "touch up inside" delegate. This delegate will execute your showNavigationController method from ViewController (using a NSNotificationCenter, as example)

Resources