Common XIB in multiple View Controller in iPhone - iphone

I have an iPhone Application with multiple view controllers , in all view controller's header is common.
I dont want to use the same method and create common view in all controllers.
So My questions is how do i use this common view in all controllers.

Use initWithNibNamed:"name of your xib" when you alloc your new view controller. It's simple.

Same but we can avoid defining a variable -
[self.view addSubview:[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] view]];

ok so you have to create it in Application Delegate once.
in .h
#property(nonatomic,strong) uiviewcontroller headerview;
in .m
#synthesize headerview=_headerview;
then alloc it in "didFinishLaunchingWithOptions" in appdelegate as singleton
self.headerview = [[headerview alloc] initWithNibName:#"headerview" bundle:nil];
So every time you want to add it to your view.
Create object from application delegate in your class after import it.
applicationdelegate app = [uiapplication sharedapplication]delegate];
[self.view addsubview:app.headerview.view];

Related

Reference view to app delegate using storyboards

I am trying to create a UITableView using storyboard but I came to something that at the end may be easy but I have no idea how to solve it.
First of all let me point out that I know that one of the limitations of storyboards is that you will have to dig through the storyboard to find information about a view you have and link it to the app delegate.
I have create my mutable array and the information that I will use in the table in the app delegate and now I want to reference that UITableView to the app delegate. The hierarchy goes like that
First I have the root view that once you click on a button it will redirect you to the second view
Inside the second view there is another button that once you press it it will redirect you to the UINavigationController
The UINavigationController contains the UITableView.
Therefore as you can see there are two views before the navigation control and the UITableView.
Here is the code I am trying to use but it does not work
UIViewController *viewController = (UIViewController *)self.window.rootviewController;
// The next line refers to the second view but does not work at all
UIViewController *secondView = [[UIViewController viewController] objectAtIndex:1];
//Then the following line is to redirect from the second view to the navigation controller
UINavigationController *navigationController =[[secondView viewController] objectAtIndex:0];
//Then is the table view
BuildingsViewController *buildingsViewController = [[navigationController viewControllers] objectAtIndex:0];
The above code does not work. Can anyone please help me?
Thanks a lot
If this code is in the app delegate there are a variety of reasons why it will probably not work. Firstly you appear to be mixing up View's, ViewControllers and Navigation controllers with what you are trying to do. Secondly there is no guarantee at the time you are trying to do this that all of the views/viewcontrollers have yet been created yet or are joined in the way they will be when the final building view controller is rendered.
What you could try instead is in your BuildingsViewController (which you say is your table view controller) you can get a handle to the App Delegate by using
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
Once you have a handle to the delegate you can simply reference your mutable array structure etc. that you created on it from within your BuildingsViewController.
e.g. in the 'numberOfRowsInSection' method:
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
NSMutableArray *myBuildings = myAppDelegate.buildingArray;
return [myBuildings count];
Or in the cellForRowAtIndexPath method:
// something like this but using your names for app delegate, building array and the accessor for the building name
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
NSMutableArray *myBuildings = myAppDelegate.buildingArray;
cell.textLabel.text = [myBuildings objectAtIndex indexPath.row].theBuildingName;

Change from UIViewController to UITableViewController

This one make me go crazy. I am building an iphone app where the first view is a login view. UIViewController, when the user succesfully logs in i want to display i table view. Somehow i just have big problems doing this.
In my app delegate i load my loginViewController, and then i want from the loginViewController load my listViewController.
What is the logic behind switching to a UITableViewController from a UIViewController?
you'd better to do it in your app delegate and surely NOT add the UITableViewController.view to the UIViewController.view... just add it to the UIWindow and then dismiss the old UIViewController (removeFromSuperView it's view and then release it)
EDIT:
that's how i manage:
i add a method in my appDelegate:
-(void)switchMainView;
and from my UIViewController i just call it with this:
[[[UIApplication sharedApplication] delegate] switchMainView];
in switchMainView i just
remove my UIViewController.view from superview,
release UIViewController,
alloc the UITableViewController and init it, then
add its view to the window app:
-(void)switchMainView{
if (mainView!=nil){ // mainView is the UIViewController
[mainView.view removeFromSuperview];
[mainView release];
mainView = nil;
}
Menu *vc; // Menu is my class, subClass of a UITableViewController
vc = [[Menu alloc] init];
nc = [[UINavigationController alloc] initWithRootViewController:vc];
[window addSubview:nc.view];
[vc release];
}
and then i do the same for going back, eventually
Assuming you already have your custom UITableViewController created:
YourTableViewController *vc = [[UITableViewController alloc] initWithStyle:...];
[self presentModalViewController:vc animated:YES];
[vc release];
you can use either i do'nt think there is a major impact but definitely they might have some advantage/Disadvantage over other..
for better understanding read the below tutorial.
http://cocoawithlove.com/2009/03/recreating-uitableviewcontroller-to.html

how to add my view controller nib file as a sub view of another rootview controller

i am having vie controller nib file with its classes .h and .m and i want to add this view as a sub view to my rootview controller innavigation controller
how to add it..
i want a syntax to this ..
please help me....
Here is the typical code that I use to add a new view controller to my navigation controller:
NextViewController *controller = [[NextViewController alloc]
initWithNibName:#"NextViewController" bundle:nil];
[appDelegate.navController pushViewController:controller animated:YES];
[controller release];
Substitute in your own view controller class name for NextViewController above, and your own NIB file name in place of "NextViewController" above.
In my applications, the navigation controller is defined and created in my application delegate class, so appDelegate above is arrived at with the following statement:
MyAppDelegate *appDelegate;
appDelegate = [[UIApplication sharedApplication] delegate];
And you would of course substitute your app delegate class name in place of MyAppDelegate.

How to get a UIViewController from a UIView via code [duplicate]

This question already has answers here:
Get to UIViewController from UIView?
(29 answers)
Closed 8 years ago.
There is a way to get a view controller reference from a UIView object? I need something like this:
MyParentViewController *myParentViewController = [self.view.superview controller];
You can use the -nextResponder method to do it. According to http://developer.apple.com/library/ios/documentation/uikit/reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/nextResponder , "UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t)"
UIView does not have reference to UIViewController by default.You can add it to your UIView subclass and set it when you create UIView in UIViewController.
If you are looking for parent of the viewcontroller, each UIViewController has property parentViewController, but if you want to access this from UIView you need to first get to your UIViewController.
You can see example how to create reference to your UIViewController in your subclass of UIView and how/where to set it up in View Controller Programming guide for iPhone, see section Creating the View Programmatically in Defining a Custom View Controller Class, here is the example, for more details see the linked Metronome example.
- (void)loadView {
self.wantsFullScreenLayout = YES;
MetronomeView *view = [[MetronomeView alloc]
initWithFrame:[UIScreen mainScreen].applicationFrame];
view.metronomeViewController = self;
self.view = view;
self.metronomeView = view;
[view release];
}
In header:
#interface MetronomeView : UIView {
MetronomeViewController *metronomeViewController;
...
You can use
[(MyParentViewController *)[[self.view superview] nextResponder] doSomething];
You shouldn't save the reference to the view controller as it may change dynamically.
Traverse the responder chain every time you need it.
You can use the following:
UIViewController* yourViewController =
(UIViewController*)[(YourAppDelegate*)
[[UIApplication sharedApplication] delegate] viewController];

Shake to open view modally

I have my 'shake' working fine (using motionEnded), based off of Apple's GLPaint code. When the user shakes the device (running 3.0 and up) I want to open a view controller modally using presentModalViewController.
In my appdelegate I have the notification (as per the GLPaint sample code):
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(shakeToOpenHiddenScreen) name:#"shake" object:nil];
In my shakeToOpenHiddenScreen I just want to open view 'x' modally but I don't think that my appdelegate will respond to presentModalViewController.
Is there a way around this?
To use presentModalViewController you have to use it from a UIViewController class, or subclass:
For example:
//RootViewController.m
[self.navigationController presentModalViewController:loginRegView animated:YES];
You can way around this problem by defining a navigation controller into your app delegate:
//yourApp_comAppDelegate.h
UINavigationController *nav;
...
#property(nonatomic,retain) UINavigationController *nav;
and synthesize it
#syntetize nav;
To use presentModalViewController you have to use it from a UIViewController class, or subclass:
For example:
//RootViewController.m
[self.navigationController presentModalViewController:loginRegView animated:YES];
You can way around this problem by defining a navigation controller into your app delegate:
//yourApp_comAppDelegate.h
UINavigationController *nav;
...
#property(nonatomic,retain) UINavigationController *nav;
synthesize it
//yourApp_comAppDelegate.m
#synthesize nav;
and now you can use the method:
//yourApp_comAppDelegate.m
[nav presentModalViewController:yourView animated:YES];
but, first you have to assign it somewhere, i will do it in the RootViewController
//RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
app = (yourApp_comAppDelegate *) [[UIApplication sharedApplication] delegate];
app.nav = self.navigationController
}
It should work, let me know :)
It is a method on UIViewController, so you should either have access to a saved view controller from your appDelegate, or else set up the notification to call one (addObserver:someVC).
"shake" isn't a standard notification name, so there should be some code elsewhere in your app that posts this notification, presumably also copied from the GLPaint sample.