Inject a NSString text to another UIViewController - ios

I'm trying to pass a NSString text to a UIViewController in button "click" event in iOS. I wrote following code.
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
self.detailViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:self.detailViewController animated:YES];
self.detailViewController.urlAddress = #"http://www.google.com";
I've already declared urlAddress property in detailViewController.
After I tested with NSLog in viewWillAppear, it shows only null values, not the urlAddress string. How can I solve it?

You're setting the string after the view controller was already presented. Switch that and you should be good.

Note the order of methods being called:
initWithNibName
viewDidLoad
viewWillAppear
Your string is changed only after all three have done their work.
Change your code to:
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
self.detailViewController.urlAddress = #"http://www.google.com";
self.detailViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:self.detailViewController animated:YES]; //Triggers viewWillAppear eventually

Related

Why can't I segue to a view controller in my XIB?

In my code below, I am getting an error saying "Property "navigationController" not found on object of type 'iPhoneFirstPageView *'. This worked before in a different project, but it won't work in this XIB. Any ideas?
-(IBAction)Twitter {
TwitterViewController *twitter = [[TwitterViewController alloc] initWithNibName:nil bundle:nil];
//[self presentViewController:twitter animated:YES completion:NULL];
//[self presentModalViewController:twitter animated:YES];
[self.navigationController pushViewController:TwitterViewController animated:YES];
}
Your first problem is that on this line:
[self.navigationController pushViewController:TwitterViewController animated:YES];
You are trying to push the CLASS TwitterViewControllerrather than your object twitter
Your second problem is that iPhoneFirstPageView is a subclass of UIView and not of UIViewController. The cleanest way for someone new to fix this would be in Xcode to create a new file called iPhoneFirstPageViewController that is a subclass of UIViewController.
The solution setup your initial view controllers as the follow:
iPhoneFirstPageViewController *firstViewController = [[iPhoneFirstPageViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
////Skip to the method you are trying to write. This will be in iPhoneFirstPageViewController.m
-(IBAction)Twitter
{
TwitterViewController *twitter = [[TwitterViewController alloc] init];
[self.navigationController pushViewController:twitter animated:YES];
}

Popover is shown null

I'm trying to show a popover with a title, creating a root view controller and instantiating it with my viewController. But, when I show the popover, the content is not shown. Here is the code:
UIViewController *popContentViewController = [[sb instantiateViewControllerWithIdentifier:#"videosTutoriais"] init];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:popContentViewController];
_popover = [[UIPopoverController alloc] initWithContentViewController:controller];
_popover.delegate = self;
[popContentViewController release];
[controller release];
//dados.myPopoverController = popOverController;
[[self popover] presentPopoverFromRect:ancora.bounds inView:ancora permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Just to be more specific, the title of popover appears normally, but the content doesn't.
Just double check that popContentViewController is not nil. You have the identifier #"videosTutoriais" which looks like it might be a scanning error.
From your code, you are releasing the controller before you display in on the screen. Try releasing it after or in the dealloc method. If you are unsure about memory management. I suggest you try ARC instead to avoid silly errors and mistakes such as this.
The code looked like this:
sb = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle:nil];
UINavigationController *controller = [[sb instantiateViewControllerWithIdentifier:#"navTutorial"] init];
_popover = [[UIPopoverController alloc] initWithContentViewController:controller];
[[self popover] presentPopoverFromRect:ancora.bounds inView:ancora permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[controller release];

UIPopoverController + UINavigationController = Delegate problems

I have two views setup ( inside a TabBar). The DetailView with a button that calls a PopOver with a NavigationController+UITableView (RootView) loading data from CoreData. I have a problem passing data from the UITableView to the DetailView. I have a protocol declared in RootView and used in the DetailView.
Here is the code I use to create the PopOver from the button because I think I have some delegate issues. Any help will be amazing,
- (IBAction)zoneListButtonController
{
if (self.controladorPopOver == nil) {
ipadrootviewController = [[iPadRootViewController alloc] initWithNibName:#"iPadRootView" bundle:[NSBundle mainBundle]];
UINavigationController *ipadnavController = [[UINavigationController alloc]
initWithRootViewController:ipadrootviewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:ipadnavController];
self.controladorPopOver = popover;
popover.delegate = self;
self.title = #"Countries";
popover.popoverContentSize = CGSizeMake(320, 300);
[self.controladorPopOver presentPopoverFromRect:CGRectMake(112, 20, 86, 27) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[ipadnavController release];
[controladorPopOver release];
}
}
An instance of the SubZone1iPadController doesn't exist when you create the popover in DetailView so you can't set its delegate property directly from the DetailView.
One option is to also add the delegate property to the iPadRootViewController which you can set in the zoneListButtonController method. Then, when ipadrootviewController creates the SubZone1iPadController, pass along the delegate.
So in both ipadrootviewController and SubZone1iPadController, add a delegate property:
#property (nonatomic,assign) id <SubZone1Tap> delegate;
Then, in the zoneListButtonController method, set the delegate property on iPadRootViewController:
ipadrootviewController = [[iPadRootViewController alloc] init...
ipadrootviewController.delegate = self;
Then, where ipadrootviewController creates SubZone1iPadController:
SubZone1iPadController *sz1 = [[SubZone1iPadController alloc] init...
sz1.delegate = self.delegate;
[self.navigationController pushViewController:...
[sz1 release];
Finally, in the DetailView, make sure the delegate method is implemented. For example:
-(void)SubZone1Tap:(NSString *)name
{
NSLog(#"SubZone1Tap, name = %#", name);
//dismiss the popover if that's what you need to do...
[controladorPopOver dismissPopoverAnimated:YES];
}

passing an NSMutableArray to an other View

my problem is this I fill my Array in my rootView and want to pass this Array to the mainView View.
I tryed it this way:
mvController = [[mainViewController alloc] initWithNibName:#"mainViewController" bundle:[NSBundle mainBundle]];
mvController.listOfContent == self.listOfContent;
[self.navigationController pushViewController:mvController animated:YES];
mvController = nil;
[mvController release];
did I missed some thing?
In theory, it's correct, except because of the "release", that must be before "mvController = nil" (otherwise, you will be releasing "nil").
You must be more specific with your error, but probably the mistake regards using "listOfContent" inside viewDidLoad or init.

Why won't self.navigationController pushViewController work?

I have a
UIViewController-> UINavigationBar + UITableView
Just a bit more explanation
I made it through UIBuilder..
1: Created a New UIViewController with XIB-file
2: Using UIBuiler i put a UINavigationController
3: Then i put UITableView underneath the navigationBar
so it gave me..
A: UIViewController-> UINavigationBar + UITableView
Now i am loading the data in UITableView from a Webservice which is working fine.
I again made a xib with sam config which is
B: UIViewController-> UINavigationBar + UITableView
So now when i try to push view B on view A using below code...it wont at all work...
SelectSiteViewController *siteViewController = [[SelectSiteViewController alloc] initWithNibName:#"SelectSiteViewController" bundle:nil];
[self.navigationController pushViewController:siteViewController animated:YES];
When i checked the UINavigationController *nav = self.navigation
nav is 0x0 that is i assume NIL.
Can anybody tell me whats wrong in here.. why is it nil...and how can i make it work..
Thanks a Lot....I would really appreciate any help
In UIBuilder verify that UINavigationController is referenced by the File's owner.
Got it.
I changed the Architecture a little bit.
I made a New UIViewController class with its xib. And coded new UINavigationController.
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *navigationController
navigationController = [[UINavigationController alloc] init];
[self.view addSubview:navigationController.view];
switch (whichViewController) {
case 1:
viewController = [[xxxx alloc] init];
break;
case 2:
viewController = [[xxx1 alloc] init];
break;
default:
break;
}
[navigationController pushViewController:viewController animated:NO];
[viewController release];
}
And pushing the view in the Switch Statement....
I hope this makes sense ......
Thanks jamihash
So you are adding the tableview to the navigation controller right? This is how:
tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:tableView animated:NO];
The tableview gets added as the rootview to the navigation controller. And then on selecting a row if you wish to push another viewcontroller use the
self.navigationController pushViewController: newViewController animated:YES];
inside the didSelectRowAtIndex method.
NOTE: its UITableViewController *tableView and UINavigationController *navigationController by declaration. So code accordingly for your table.
why UIViewController-> UINavigationBar + UITableView ?
I suggest you another approach
->UITableViewController A -> Embedded with a Navigation Controller, then after populate tableview you can pass data to
->UITableViewController B by
[[self storyboard]instantiateViewControllerWithIdentifier:#"ControllerB"];
then, in storyboard, drop a tableView B and in Identity->storyboard Id put some id.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sUrl= #"<#string#>";
if (indexPath.row == 0) sUrl= #"http://www.apple.com/";
if (indexPath.row == 1) sUrl= #"http://www.google.com/";
if (indexPath.row == 2) sUrl= #"http://www.times.uk/";
NSMutableArray *selectedObject = [arrayOpenMale objectAtIndex:0];
NSLog(#"%# is the selected object.",selectedObject);
SeriesAdetailVC *dvc = [[self storyboard]instantiateViewControllerWithIdentifier:#"DetailView"];
dvc.strings7 = [NSURL URLWithString:sUrl];
[self.navigationController pushViewController:dvc animated:YES];
}
hope help

Resources