How to set title for Navigation Bar - uinavigationcontroller

How to set the title on a root viewcontroller inside a navigation viewcontroller. Generically, the answer seems to be, "set self.title of some viewcontroller". but this only works for the viewcontrollers I am pushing to. On the initial viewcontroller, running self.title = #"my title"; or self.navigationController.navigationItem.title = #"my titles"; don't work.
Thinking that is needed to be set earlier on, I did this in app delegate:
MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] initWithNibName:#"MainMenuViewController" bundle:nil];
mainMenuViewController.title = #"coding chal";
self.navController = [[UINavigationController alloc] initWithRootViewController:mainMenuViewController];
What must I do for the root viewcontroller to have a title?Thanks in advance

Setting the view controller's title does work. When you run into trouble, the best course is make yourself a minimal test example. Here's one. This is the only code in the app!
In the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [UIWindow new];
ViewController* vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
In the view controller:
- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self.title = #"Testing";
return self;
}
Result:
So you can see that it really does work. If it seems not to work in your app project, therefore, that must be because of other code somewhere.

Related

UINavigationBar does not show on second ViewController on Apple TV

I have the following code in AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController* viewController = [sb instantiateViewControllerWithIdentifier:#"ViewController"];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
UILabel* topBar = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
topBar.text = #"Custom Navigation";
[navigationController.navigationBar.topItem setTitleView:topBar];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;
}
When the button is clicked, the following code is run;
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController* second = [sb instantiateViewControllerWithIdentifier:#"second"];
[self.navigationController setNavigationBarHidden:NO];
[self.navigationController pushViewController:second animated:YES];
The navigation bar is always hidden on the second screen. I tried [self.navigationController setNavigationBarHidden:NO] and [self.navigationController.navigationBar setHidden:NO] but it does not work.
My code runs successfully on iPhone or iPad, but does not work on Apple TV.
What is the solution to show the navigation bar on the second screen?
Don't use UINavigationBar on Apple TV. UITabBar is more useful. If you make custom tab bar, implement UITabBarController to your interface and write fallowing code to the viewDidLoad method.
customView = [[[NSBundle mainBundle] loadNibNamed:#"CustomTopBar" owner:self options:nil] objectAtIndex:0];
[self.view addSubview: customView];

iOS 6.1 - How do I implement imagePickerControllerDidCancel for a non-modal UIImagePickerController

On iOS 6.1, I am displaying a UIImagePickerController non-modally in one tab of a UITabBar.
In my init:
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
self.picker.delegate = self;
self.picker.allowsEditing = NO;
[self.view addSubview:self.picker.view];
I have implemented:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
return;
}
Since I never called:
[picker presentViewController:(UIViewController *) animated:(BOOL) completion:^(void)completion];
I shouldn't need to call:
[picker dismissViewControllerAnimated:NO completion:nil];
But, when I press the Cancel button, the Cancel button is grayed out and the UIImagePickerController seems to lock up. Some of the controls work, like the image/video switch and the reverse camera button, but the button to take a picture is frozen.
If I go to a different tab and return to the Camera tab, the UIImagePickerController is reset and fine again. The only code executed in this case is viewWillAppear and viewDidAppear which shouldn't have anything relevant to this situation.
On iOS 7,nothing locks up when the Cancel button is pressed.
Since the UIImagePickerController is always displayed in the tab, I don't really have a need for a cancel button so how would I either:
Hide or disable the Cancel button
Implement imagePickerControllerDidCancel so things do not lock up
Implemented a Camera Overlay View and things work fine.
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
self.picker.delegate = self;
self.picker.allowsEditing = NO;
self.picker.showsCameraControls = NO;
UIView *clearView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
clearView.opaque = NO;
clearView.backgroundColor = [UIColor clearColor];
UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-self.tabBarController.tabBar.frame.size.height-55, self.view.frame.size.width, 55)];
toolBar.barStyle = UIBarStyleBlackOpaque;
NSArray *items=[NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(takePicture)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
nil];
[toolBar setItems:items];
UIView *overlayView = [[UIView alloc] initWithFrame:self.view.bounds];
[overlayView addSubview:clearView];
[overlayView addSubview:toolBar];
[self.picker setCameraOverlayView:overlayView];
[self.view addSubview:self.picker.view];
[[UIBarButtonItem alloc] ... action:#selector(takePicture)],
- (void)takePicture
{
[self.picker takePicture]; // triggers didFinishPickingMediaWithInfo
}
UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
if (image != nil)
{
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
}

loading another tableview when a tablecell of one tableview is clicked returning error:"Program received signal SIGABRT"

I am creating an app in which there are two UIViews and in those UIViews I am loading Tableviews..
When I click a tablecell in one TableView then I am unable to redirect it to another TableView and getting error:Program received signal SIGABRT.But if I want to load a UIView when a tablecell is clicked it gets executed perfectly.I couldn't understand where Am I going wrong....
This is the code i'm writing
ViewController1:
#import ViewController2.h"
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ViewController2 *v2 = [ViewController alloc] initWithNibName:#"ViewController2" bundle:[NSBundle mainBundle]];
[self presentModalViewController:v2 animated:NO]; **//getting error at this line**
[v2 release];
}
ViewController2.h
#import"ViewController1.h"
- (void)viewDidLoad
{
[super viewDidLoad];
tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(10, 10, 320, 460)];
tableView1.delegate = self;
tableView1.dataSource = self;
[self.view addSubview:tableView1];
}
Couldn't understand what could be the possible cause of this error..
Maybe this is the bug
ViewController2 *v2 = [ViewController alloc] initWithNibName:#"ViewController2" bundle:[NSBundle mainBundle]];
You have allocated ViewController instead of ViewController2
Try this and it should work, I guess.
ViewController2 *v2 = [ViewController2 alloc] initWithNibName:#"ViewController2" bundle:[NSBundle mainBundle]];

How to log the backbutton that comes with the navigationcontroller

I am creating a new view using
[[self navigationController]pushViewController:newViewController animated:YES];
The new view (newViewController) stacks on the top of the previous, and comes with a backbutton. I need to access this backbutton to override it. To begin with, finding out how to NSLog a string when the back button is pushed will be of great help.
Best, Tom
I found the answer myself.
I have to make a new backbutton in the viewDidLoad function, and add a selector.
-(void) viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"myTitle"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(mySelector:)];
self.navigationItem.leftBarButtonItem = backButton;
}
Then the selector:
-(void)mySelector
{
NSLog(#"backButton pushed");
}

UILabel to NSString to load webview

So I have this code, where I am sending a variable storyLink to a label and display it in the specified nib. So far the label displays the text which is what I want. But I want to use that text as an url for a webview. urlString = nothing as of right now because I can't figure out how to use/convert an UILabel to a String suitable for a webview url. So if that was confusing basically I want to take the text stored in my label and use it for the url in my webview. Can someone help?
TestView.h
#interface TestView : UIViewController {
IBOutlet UILabel *label;
IBOutlet UIWebView *webView;
NSString *urlString;
}
#property(nonatomic, retain)IBOutlet UILabel *label;
#property(nonatomic, retain)IBOutlet UIWebView *webView;
#property(nonatomic, retain)NSString *urlString;
#end
TestView.m
- (void)viewDidLoad {
[super viewDidLoad];
label.text=nil;
urlString=something;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
sending code
TestView *objFirstController=[[[TestView alloc] initWithNibName:#"TestView" bundle:nil] autorelease];
[self.navigationController pushViewController:objFirstController animated:YES];
objFirstController.label.text=storyLink;
When setting properties that are accessed during 'viewDidLoad', make sure to put them before you use pushViewController.
TestView.m
- (void)viewDidLoad
{
[super viewDidLoad];
label.text=urlString;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
sending code
TestView *objFirstController = [[[TestView alloc] initWithNibName:#"TestView" bundle:nil] autorelease];
objFirstController.urlString = storyLink;
[self.navigationController pushViewController:objFirstController animated:YES];

Resources