I'm pretty new to Objective-C and I'm working on a webradio app.
My app is composed of some NavigationControllers included in a TabBarController.
I want to make a View wich would stay just above the TabBar ALL THE TIME. (it will contain the audio player controls and must be accessible anywhere in the app)
What would be the best way to do so?
Thanks!
SQ;p
You need to add your view as a subview of the UITabBarController view property:
m_yourToolbar =[[UIToolbar alloc] initWithFrame:CGRectMake(0, 401, 320, 44)];
// set some properties on the toolbar
// ...
[self.tabBarController.view m_yourToolbar];
This adds the UILabel blah over the content for every tab in the UITabBarController (m_tabBarController).
#interface YouAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIToolbar * m_yourToolbar;
// ... whatever other stuff you have in your app delegate
}
#property (nonatomic, retain) UIToolbar * yourToolbar;
In your app delegate implementation you will need:
#synthesize yourToolbar= m_yourToolbar;
// .. other app delegate stuff
So in your view controllers that need to update the toolbar, you can get hold of the app delegate, grab the yourToolbar property and set attributes on it:
AppDelegate *appDelegate = (YouAppDelegate *)[[UIApplication sharedApplication] delegate];
// set stuff on the toolbar property
appDelegate.yourToolbar.stuff = stuff;
Related
I have been searching and experimenting on this for a while, but I just can't find out what I'm doing wrong.
I want to make an app with a menu, and from that menu, you can get to a TableViewcontroller with a title bar. I found out that, in order to get this title bar, you need to "insert" UITableViewController into a UINavigationController. this is where I'm stuck.
let's not mind / forget the menu from the app for now, because i know how to switch view controllers when user taps a button.
in my AppDelegate.m I have:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
TableViewController *tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
self.navController = [[UINavigationController alloc] initWithRootViewController:tableView];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
in my AppDelegate.h file I Have:
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
UINavigationController *navcontroller;
}
#property (strong, nonatomic) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
I have 2 classer, UITableViewController and NavigationView.
In the UITableViewController, I made an array called tableRows:
tableRows = [[NSArray arrayWithObjects:#"1", #"2", nil] init];
the numberOfSectionsInTableView is set as 1 and the numberOfRowsInSection is set as return tableRows.count;
I left my NavigationView.m untouched, except that I tried to set a title in it.
in my NavigationView.xib, I tried to make the connection between the UITableViewController and the UINavigationController.
I added an NSObject, and changed its class to AppDelegate
I replaced the View for a UIWindow and connected the window property from my
AppDelegate.h to it
I added a UINavigationController and
connected it with the navController from my AppDelegate.h.
and last, I changed the class from the RootView from the
UINavigationController to UITableViewController.
Now my problem is, with or without the connections in IB, whatever I try to change in my table, or in the titlebar, it does not change when I run the app.
Does anybody know what I'm doing wrong? I'm using xcode 4.6, so lots of the tutorials I've checked are not very useful, because they are made with older versions of xcode. please help me, Thank you in advance!
in you AppDelegate replace this line
TableViewController *tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
with this
TableViewController *tableView = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];
You're not creating an instance of TableViewController but instead UITableViewController so thus you're not getting any of your code executed. (You should have a compiler warning for this in your app delegate - make sure to heed those warnings). Likewise you're not loading the NavigationView nib anywhere - you're creating a navigation controller with the table as the root view controller. It's not clear what your objective for NavigationView is so I can't provide a recommendation for how to proceed. However, for the table view, change the following line in your app delegate
TableViewController *tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
to
TableViewController *tableView = [[TableViewController alloc] init];
In your table view xib, make sure File's Owner's class is set to be of class TableViewController (not UITableViewController). Right click on the table view and drag the circles next to 'data source' and 'delegate' to file's owner. Then right click file's owner and make sure the circle next to 'view' is connected to the table view. Save, clean, and run.
You might also find it very useful to checkout the free Sensible TableView framework. The framework will take your array and automatically generate all the table view cells, while managing all the details required to correctly setup the navigation controller. Hope this helps.
I am new to Iphone and I have started an application in which I have added a custom tab bar which has to load to some particular page only. The tab bar works as per my expectation. Now the problem is that, when I navigate to other pages the tab bar keeps on showing and it cause serious problem for me...
Here is my implementation
In .h:
#import <UIKit/UIKit.h>
#class MainMenuViewController;
#interface RoutineListViewController : UIViewController<UITabBarDelegate>{
MainMenuViewController *homeBtn;
UITabBar *mainTabBar;
UIViewController *routineTabViewController;
UIViewController *calendarTaViewController;
UIViewController *editTabViewController;
}
#property (nonatomic, retain) IBOutlet UITabBar *mainTabBar;
#property (nonatomic, retain) IBOutlet UIViewController *routineTabViewController;
#property (nonatomic, retain) IBOutlet UIViewController *calendarTabViewController;
#property (nonatomic, retain) IBOutlet UIViewController *editTabViewController;
- (IBAction)goToHome:(id)sender;
#end
In .m, i am implementing the tab as:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case 1:
if (routineTabViewController == nil) {
self.routineTabViewController =[[RoutineListViewController alloc] initWithNibName:#"RoutineListViewController" bundle:nil];
[self.view insertSubview:routineTabViewController.view belowSubview:mainTabBar];
routineTabViewController = nil;
[routineTabViewController release];
}
break;
case 2:
if (calendarTabViewController == nil) {
self.calendarTabViewController =[[CalendarTabViewController alloc] initWithNibName:#"CalendarTabViewController" bundle:nil];
[self.view insertSubview:calendarTabViewController.view belowSubview:mainTabBar];
calendarTabViewController = nil;
[calendarTabViewController release];
}
break;
case 3:
if (editTabViewController == nil) {
self.editTabViewController =[[EditTabViewController alloc] initWithNibName:#"EditTabViewController" bundle:nil];
[self.view insertSubview:editTabViewController.view belowSubview:mainTabBar];
editTabViewController = nil;
[editTabViewController release];
}
break;
default:
break;
}
}
And when I implement a button to go to some other page, the tab bar keeps showing. Here is the button implementation in EditTabViewController.m file.
- (IBAction)goToHome:(id)sender {
homeBtn = [[MainMenuViewController alloc] initWithNibName:#"MainMenuViewController" bundle:nil];
[self.view addSubview:homeBtn.view];
}
Per Apple's rules, a TabBarController is supposed to be the main container. Plus, if you just implement a custom UITabBar, are you using that to push the user into other views? If so, if the UITabBar disappears, how does the user ever return?
If you only want the bar visible when a particular page is visible, why not implement a UINavigationController (as the main container) and have the root UIViewController implement a UIToolbar that performs the same functions. Then, when you navigate to another page (I'm assuming here that you mean a new screen, not a different page in a UIPageControl or a web page in a UIWebView), you push in a new UIViewController that doesn't contain the UIToolbar.
Kind of like this:
--UINavigationController
|
-->UIViewController as RootViewController --> Contains UIToolbar
|
-->Pushes UIViewController --> Has no UIToolbar
Edit/Update
I just saw your code, and I'm not sure I understand what you're trying to achieve. I think you might be trying to somehow implement behavior similar to a UINavigationController without actually using one.
Edit/Update #2
I think you are wanting behavior that can be implemented like this:
UINavigationController (containing IconMenuViewController as RootViewController)
|
--> PageViewController (push into this from any icon touch in IconMenuViewController)
--> Contains UIToolbar/UITabBar
If you use the hierarchy above, the UINavigationController will automatically provide you with the NavigationBar at the top of the screen and give you a back button. As long as you make the UIToolbar or UITabBar part of the PageViewController, it should appear and disappear with its view controller as you push and pop it. Does that make sense?
I'd just like to clear something up..
I have an app where the Main Window UI has a Tab bar with 3 tabs (opt1, opt2, op3). Each opt has its own xib file where i've drawn their own interfaces.
In my app delegate class I have included a UITabBar *rootController, and hooked this up to my tab bar in my Main Window xib file.
Now.. In the Tab bar, I have dragged in 3 navigation controllers (1 for each opt) and inside each one I have a 1) tab bar icon, 2) navigation bar and 3) view controller.
Back in my app delegate.h class I have included code for UINavigationController *nav1, nav2, nav3..and hooked these up accordingly in IB in MainWindow.xib (TabBar->navController1, navController2, navController3).
Is this the right way to do it? Also how can I make use of these nab bars in my opt1, opt2, opt3 class files?
here is my code:
app delegate.h
#import <UIKit/UIKit.h>
#class LoginViewController;
#interface myAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UINavigationController *navigationController1, *navigationController2, *navigationController3;
IBOutlet UITabBarController *rootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController1, *navigationController2, *navigationController3;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#end
appdelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:[rootController view]];
[window makeKeyAndVisible];
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:nil];
[self.rootController presentModalViewController:loginViewController animated:NO];
}
Then in my LoginController.m class , when the user enters correct credentials I call
[self dismissModalViewControllerAnimated:YES];
In my MainWindow.xib, I hook up my rootController to a TabBarController. In the TabBarController I have put 3 NavigationControllers inside it and linked them to 3 tabOption classes which each have their own .xib view.
The tab bar switches between the 3 option views nicely. However in 1 .xib view I have a button to open a new .xib. So in my tabOption1 class I have the following:
-(IBAction)openBook:(id)sender{
UIViewController *nextVC = [[PageViewController alloc] initWithNibName:#"PageView" bundle:nil];
[self.navigationController pushViewController:nextVC animated:YES];
}
However this does not open up my PageView.xib... I have connected it to my PageViewController class and everything too..and the button works because I've tested it with a UIDialog
Have you seen the Apple Programming Guides? They might give you a better understanding of how everything ties together - you could start here:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/NavigationControllers/NavigationControllers.html#//apple_ref/doc/uid/TP40007457-CH103-SW1
In answer to your question, that looks like an OK way of setting up. I really would recommend reading up a bit though :)
In response to your comment, that looks like a reasonable way to do what you're trying to achieve. If it works, then it works.
In response to your other issue then you can get the navigation controller object by doing this: self.navigationController
So you can "go to" a new view controller like this:
// make the view controller
UIViewController *nextVC = [[MyCustomViewController alloc] initWithNibName:#"MyCustomViewController" bundle:nil];
// push it onto the navigation stack
[self.navigationController pushViewController:nextVC animated:YES];
To add this to the click event on a button you need to create the button in interface builder and create an IBAction in your code. The IBAction might look like this:
- (IBAction)pushNextViewController:(id)sender {
UIViewController *nextVC = [[MyCustomViewController alloc] initWithNibName:#"MyCustomViewController" bundle:nil];
[self.navigationController pushViewController:nextVC animated:YES];
}
Then you need to link to it from interface builder. I'm not sure how to do this, I generally don't use interface builder, and certainly haven't used it since about XCode 3.
To do it programatically you can use this method:
[MyButton addTarget:self selector:#selector(pushNextViewController:) forControlEvents:UIControlEventTouchUpInside]; // always use touch up inside
Keywords to look up to help you find tutorials and stuff on the internet: ibaction uinavigationcontroller pushviewcontroller:animated: popviewcontrolleranimated:
I have a UITableView in a UINavigationController. On the navigation bar I have a button called add. When this button is pressed it presents a UIPopoverController, where user can input data to be added as a new row/cell in the UITableView. My issue is how can I add a new cell to the UITableView from the UIPopover? Do I pass in the array data to the UIPopOver root controller?
There are two solutions to this that I'm aware of. One would be to send a notification from the popover to the root controller and apply the necessary code to update the tableView in the handleNotification method.
The other, one that I personally use, is to set up a delegate protocol for the popover. You'll have to set it up something like this:
#protocol PopoverDelegate
- (void)addNewCell; // you can add any information you need to pass onto this if necessary such as addNewCellWithName:(NSString *)name, etc.
#end
#interface MyPopoverViewController..... {
id <PopoverDelegate> delegate;
// the rest of your interface code;
}
#property (nonatomic, retain) id delegate;
// any other methods or properties;
#end
Then in your root view controller header file, you need to add the delegate
#interface RootViewController .... <PopoverDelegate> {
Then in your root view controller implementation file, assign the popover delegate when you instantiate it. For example:
MyPopoverViewController *vc = [[MyViewController alloc] init];
vc.delegate = self; // this is where you set your protocol delegate
myPopover = [[UIPopoverController alloc] initWithContentViewController:vc];
myPopover.delegate = self;
[vc release];
Finally, you'll add your protocol method somewhere in the code
- (void)addNewCell {
// do what you want with the tableView from here
}
Sorry that's a bit long. I just wanted to make sure I was thorough. Hope it helps
I have an UITabBarController with two tabs:
UINavigationController
OptionsViewController : UIViewController
How can I reach data (ie. UILabel.text) set in OptionsViewController, in a new added modal View which has been invoked from UINavigationController?
Edit1:
Scenario: After launching app I select the second tab bar called "Options" where I fill up a textField. The label is set to value from textField. Next I select first tab bar called "Main" where I have a button. I click the button and new modal View appears. In this new modal View I'd like to show the value from textField
I love MVC, but I'm not an absolute purist to the point of hurting yourself to accomplish a fairly trivial task, so the answers you've gotten here are good and useful. However, by creating an ivar to refer back to a specific type such as a label or other view controller, you are coupling things together that aren't necessary to couple. What you could do instead is make your first tab view controller a delegate of your second tab view controller. So do something like this in your app delegate.
OptionsViewController *optionsViewController = // ... get this from the tab view
FirsTabViewController *firstTabViewController = // ... same here
[optionsViewController setDelegate:firsTabViewController];
Which means that you need an ivar in your OptionsViewController:
#property (assign) id delegate;
Then, when whatever event you want to trigger the change occurs in your options view controller, see if the delegate can respond to a selector you've named. For example:
- (void)someEventHappenedLikeTyping:(id)sender;
{
if ([delegate respondsToSelector:#selector(setOptionsString:)]
[delegate performSelector:#selector(setOptionsString:) withObject:[label text]];
}
Notice you never specified any specific object types. You just check to see if the delegate (which was declared as id) can respond to that selector. If it can, it does what it's told and just is silent otherwise.
For this to work, you need an ivar for the optionsString in your FirstTabViewController and so it would be declared in the header as:
#property (copy) NSString *optionsString;
and then #synthesize it in the .m. This causes -setOptionsString to become a valid selector that will get called in the -someEventHappenedLikeTyping method.
Anyhow, now, if you ever need to to change which view controller references which, you don't have to go into the header and change the type of ivar referenced. You simply need to implement the selector (this is known as an informal protocol, by the way) in the view controller that is a delegate of your options view controller.
Just some food for thought there. Hope that helps. There is further de-coupling that could be done in the code I've added, but again it may be overkill for such a simple task. Let me know if you need clarification or want to understand what I mean by further decoupling.
Best regards,
p.s. Sometimes needing to share data between two tab bar view controllers, means you have a design flaw. If you are wanting to store preferences from your options view, you should just call
[[NSUserDefaults standardUserDefaults] setObject:[label text] forKey:#"option1"];
[[NSUserDefaults standardUserDefaults] synchronize];
Then you can pull from the NSUserDefaults back in your main tab with;
NSString *option1 = [[NSUserDefaults standardUserDefaults] objectForKey:#"option1"];
// Do something with option1
In your OptionsViewController, create a property:
#property (nonatomic, retain) UILabel *mylabel;
then after creating your OptionsViewController, but before displaying it, set the mylabel property. (Or perhaps you just want the text, so you can use an NSString* property.)
Edit:
So you probably want to do something like this:
OptionsViewController *vc = [[OptionsViewController alloc] init];
vc.mylabel = mySomethingLabel;
[self presentModalViewController:vc animated:YES];
So after creating the object, you set the property, and then you display the view controller.
I usually set IBOutlets in each of my viewcontrollers which point to the other controller.
So if I had view controllers A and B. A has an IBOutlet to B and B to A. Then whenever I want to access anything in B from A i just use a dot operator on B.
In your example UINavigationController would #include "OptionsViewController.h" and have an ivar IBOutlet OptionsViewController * ovc (which is set in IB) and then any instance variable from your options view controller can be referenced as ovc.UILabel.text from the navigation controller. This process can be reversed to access values from your navigation controller in your options view controller.
Example Navigation Controller (.h):
#include "OptionsViewController.h"
#interface UINavigationController // (whatever the name of this class is)
{
OptionsViewController * ovc;
}
#property (nonatomic, retain) IBOutlet OptionsViewController * ovc;
#end
Example OptionsViewController.h:
#interface OptionsViewController
{
UILabel * label;
}
#property (nonatomic, retain) IBOutlet UILabel * label;
#end
Then from UINavigationController (.m) you can just write ovc.label.text to access the text.
I have an easy way to access data between views. Let's try. You have two views named view1 and view2, you can define a View *vc1 property in view2.h, set the vc1 point to view1, when pop the view2, like this:
view1.m
//here pop out view1 code
View2 *view2 = [[View2 alloc] initWithNibName:#"View2" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:view2 animated:YES];
view2.vc1 = self; //transfer view1 instance to view2,use vc1 you may handle view1 in view2 directly
[view2 release];
view2.h
#import "view1.h"
#property (nonatomic, retain) IBOutlet View1 *vc1; //here you could name it as view1 as well :)
view2.m
vc1.lblTable.text = #"ok"; //you'll see "ok" in view1
[self.navigationController popViewControllerAnimated:YES]; //navigate to view1
//dont forget release vc1