How to log the backbutton that comes with the navigationcontroller - uinavigationcontroller

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");
}

Related

How to set title for Navigation Bar

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.

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]];

navigation inside tabbar which is called modally from a didSelectRowAtIndexPath

I am creating an app as follows- when the app starts a tableview is displayed. I am creating this programmatically. On selecting a particular row i do a presentModalViewController to display my tabbar class which i created programmatically.
ViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ShowOptionInTab *showTabbar = [[ShowOptionInTab alloc] initWithNibName:#"ShowOptionInTab" bundle:nil];
UINavigationController *mynavController = [[UINavigationController alloc] initWithRootViewController:showTabbar];
[self presentModalViewController:mynavController animated:YES];
[showTabbar release];
}
ShowOptionsInTab.m class
#implementation ShowOptionInTab
-(void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissTabbar:)];
self.navigationItem.rightBarButtonItem=doneButton;
UITabBarController *tabbarController = [[UITabBarController alloc] init];
tabbarController.view.frame = CGRectMake(0, 0, 320, 460);
BuyerViewController *buyerController = [[BuyerViewController alloc] init ];
buyerController.navigationItem.title=#"Buyer";
buyerController.title=#"Buyer";
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
SellerViewController *sellerController = [[SellerViewController alloc] init];
sellerController.navigationItem.title=#"Seller";
sellerController.title=#"Seller";
LenderViewController *lenderController = [[LenderViewController alloc] init];
lenderController.navigationItem.title=#"Lender";
lenderController.title=#"Lender";
tabbarController.viewControllers = [NSArray arrayWithObjects:buyerController,sellerController,lenderController, nil];
[self.view addSubview:tabbarController.view];
[sellerController release];
[buyerController release];
[lenderController release];
}
-(void)dismissTabbar:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
My ShowOptionsInTab class is a subclass of UIViewController
I have 3 tabs Buyer, Lender and Seller. Each tab has buttons which navigate to a different UIViewController page. Suppose i have 'ButtonA' and 'ButtonB' in my Buyer tab class. The problem i am facing is that i cannot navigate on the buttons present in Buyers tabs(or any other tabs). It doesnt push to the next class which 'ButtonA' should load. It also has a "Done" button on navigation bar which will dismiss the modal view and display my table view.
What am i doing wrong? if i add these tabs to localNavigationController object which i create in my ShowOptionsInTab.m class i get 2 Navigations Bars.
I got the solution. I made 3 local navigationController object and added each of my tabbar class as its rootviewController.

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