On my main view controller I have a button that shows a new view.
- (IBAction)showInformation:(id)sender {
InfoViewController *controller = [[InfoViewController alloc] initWithNibName:#"InfoView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[self.view addSubview:[controller view]];
}
On my InfoView.xib I added a UINavigationController and made the proper connections. However when I the InfoViewController loads is not showing the UINavigation as I want it to.
Currently I have this on viewDidLoad
self.view = [navController view];
And on the header file I have
IBOutlet UINavigationController *navController;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
What am I missing?
UINavigation did not show you mean UINavigationController did not appear?
1 If you use UINavigationController, you need at least two views: one for the default view that appears with the controller and another that is pushed two.
I can only see one view here, which is InfoViewController.
If you use UINavigationController, you may want to use the pushViewController method.
2 If you use presentModalViewController method, no UINavigationCOntroller is needed.
Related
I am new in ios development. I want to develop custom navigation button method.
-(void) handleNext:(id)sender
{
MRGAppDelegate *appDelegate = (MRGAppDelegate*)
[[UIApplication sharedApplication]delegate];
[appDelegate.viewController GotoDirectoryView:self.restListViewController calledView:self.view];
}
in MRGViewController.m
-(void) GoToDirectoryView:(RestaurantListViewController*) resViewContrller calledView:(UIView*)viewControllerView
{
self.resListViewController = resViewController;
[resViewController release];
[viewControllerView removeFromSuperview];
[self.resListViewController viewDidLoad];
[self.view addSubview:self.resListViewController.view];
}
But Don't call to RestaurantListViewController viewDidLoad don't call. No error appear. I don't know why? Please help me.
viewDidLoad, viewWillAppear etc are view controller life cycle method, these are called automatically when view is loaded on navigation stack. You won't need to call these method at all.
Currently you are simply adding a view on existing view controller, while you need to load a new view controller if you want view controller life cycle methods to get called.
Use like -
[self.navigationController pushViewController:restListViewController animated:YES];
[restListViewController release];
EDIT 1 -
restListViewController = [[RestaurantListViewController alloc] init];
[self presentModalViewController:restListViewController animated:YES];
[restListViewController release];
I try to explain my problem.
I have a first view in which I have some buttons. Pressing a button the app open a tableview. I have those files in my project:
AppDelegate.h/m
viewcontroller.h/m to control the first view
Entity1TableViewController.h/m to control the tableview opened by that
button in 1st view
an .xcdatamodel
so, until I'm in the tableview all is right. I putted this
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:#"Ditloide" inManagedObjectContext:context];
and when I tab on the button I receive the error:
+entityForName: could not locate an NSManagedObjectModel for entity name 'Ditloide' in a second view
I read other posts in which I found something like this:
MainViewController *controller = (MainViewController *)self.window.rootViewController;
controller.managedObjectContext = self.managedObjectContext;
to put in the rootcontroller. but in my case, where can I put this? I have to create a new controller? if yes, I cant insert the
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method.
So, how can I solve the problem?
I'm not pretty sure I understand your question.
The error is due to the fact you didn't set up the MOC correctly. See insertNewObjectForEntityForName: for further details.
The question is: did you set up the core data stack correctly? Could you share some code?
Then, about the code you have seen, it has the goal to inject the context wherever it is needed. For example, suppose you have a controller, called YourController, that needs the context. You can create a property in YourController like the following:
//.h
#property (nonatomic, retain) NSManagedObjectContext* context; // or strong if you ARC
//.m
#synthesize context;
Then, from a different element, for example the app delegate (if you have set up the core data stack there), you could just create YourController and inject it.
YourController* yourCtr = // alloc-init
yourCtr.context = [self managedObjectContext];
Finally, what do you mean with if yes, I cant insert the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method?
Newbie question. I created a UIView named TestView with 2 IBOutlet UIButton. I would like to add this view to initially launched view controller.
The header file of the TestView is as follow:
#import <UIKit/UIKit.h>
#interface TestView : UIView {
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
}
#end
I try to add the view to the screen by:
TestView *view = [[TestView alloc] init];
[self.view addSubview:view];
[view release];
but I got no response. How can I add TestView to the original view ?
p.s. TestView as a XIB too, named TestView.xib
Environment:
xCode 4
iOS 4.3.2
u have to specify the frame
frame means orgin + size
here orgin (0,0)
size (200,200)
otherwise iOs dont know where to draw this view
TestView *view = [[TestView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[view setBackgroundColor:[UIColor greenColor]];
[view addSubview:btn1];
[view addSubview:btn2];
//here add whatever u want
[self.view addSubview:view];
[view release];
You shouldn't call it view because your UIView's got already an instance variable called view.
You could also create this using Interface Builder and linking your elements to your Outlets, it will be easier
I have a rootViewController like so:
Header:
#interface ParkingRootViewController : UIViewController {
UINavigationController *navigationController;
UIToolbar *toolbar;
UIBarButtonItem *lastUpdateLabel;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
}
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *lastUpdateLabel;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (nonatomic, readonly) NSString *applicationDocumentsDirectory;
-(IBAction)selectHome:(id)sender;
//-(void)loadOverlays;
-(void)testCoreData;
#end
Implementation:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//...
[self testCoreData];
//creating label in tool bar
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
label.text = #"last updated...";
label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
//label.highlightedTextColor = [UIColor colorWithWhite:0.5 alpha:1.0];
//label.highlighted = YES;
label.font = [UIFont systemFontOfSize:13.0];
label.userInteractionEnabled = NO;
[lastUpdateLabel initWithCustomView:label];
[label release];
[self.view addSubview:self.navigationController.view];
[self.navigationController.view setFrame:self.view.frame];
}
But I need to transfer my managedObjectModel to my table view and then to a map view so that the map view can make queries depending on what the user wants to see. I was consulting some apple sample code which looks like(from Recipes):
- (void)applicationDidFinishLaunching:(UIApplication *)application {
recipeListController.managedObjectContext = self.managedObjectContext;
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
I know that is in the appDelegate, but I figure that I can do the same when a row is selected or another view is pushed onto the stack, right? The problem is that I have configured most of my view with a nib which looks like so:
Due to this, I am unable to use a similar strategy that Apple employs to transfer a managedObjectModel to an alternate viewController(in this case PermitListViewController) because I don't access PermitListViewController directly when adding a subview. If anyone has any idea of how I would go about getting my managedObjectModel to my PermitListViewController. Please share! Thanks in advance!
EDIT: I am thinking of placing the managedObjectModel in a singleton class. What are your guys' thoughts on that? Good programming practice? Anything I should be aware of? Thanks.
Why not have the NSManagedObjectContext on the app delegate? Then it would be easily accessible from all your view controllers, and as they are UI they execute on the main thread, and can therefore share the same MOC.
I ended up creating a singleton class for the managedObjectModel using this as a reference (scroll down to "Creating a Singleton Instance").
Almost all the examples I see are done with IB but I don't want to use IB.
What I want to do is, when a user selects a row in the table a UIWebView will be pushed onto the stack and load that particular page while keeping the tab bar and navigation bar. I don't want all the features of a browser rather just be able to scroll through a page beacause the rest of my app controls how a person can navigate through the website through tables.
So I've been able to push other viewcontrollers but pushing a UIWebView with the same method doesn't work.
Here is what I have so far..
This is the Threads.h file
#import "ThreadContent.h"
#import <UIKit/UIKit.h>
#interface Threads : UITableViewController {
NSMutableArray *threadName;
NSMutableArray *threadTitle;
UIActivityIndicatorView *spinner;
NSOperationQueue *operationQueue;
UILabel *loadingLabel;
NSMutableDictionary *cachedForum;
NSMutableArray *forumID;
NSInteger *indexPathRowNumber;
NSMutableArray *threadID;
ThreadContent *threadContent;
}
#property (nonatomic, assign) NSMutableArray *forumID;
#property (nonatomic, assign) NSInteger *indexPathRowNumber;
#end
Part of my Threads.m file where I am trying to push the UIWebView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"%i",indexPath.row);//get row number
NSLog(#"%#", [threadID objectAtIndex:indexPath.row]);//thread id
//forums.whirlpool.net.au/forum-replies.cfm?t=
//NSString *urlString = [NSString stringWithFormat:#"forums.whirlpool.net.au/forum-replies.cfm?t=%#", [threadID objectAtIndex:indexPath.row]];
threadContent = [[ThreadContent alloc] init];
[self.navigationController pushViewController:threadContent animated:YES];
}
My WebView files.. well i'm not sure how to do that? I did make it a "UIWebView" subclass but if i try to push it on to the stack i get a crash saying it needs it to be a "UIViewController" subclass.
UIWebView is a subclass of UIView not UIViewController.
You need to subclass UIViewController (call it for example WebViewController)
And in the viewDidLoad method create a UIWebView and add it to the view, using addSubview:
You can pass the URL to load in the webView as a property of the WebViewController
-(void)viewDidLoad {
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubView:webView];
[webView loadRequest:[NSURLRequest requestWithURL:self.urlToLoad]];
[webView release];
}
and in Threads
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"%i",indexPath.row);//get row number
NSLog(#"%#", [threadID objectAtIndex:indexPath.row]);//thread id
//forums.whirlpool.net.au/forum-replies.cfm?t=
//NSString *urlString = [NSString stringWithFormat:#"forums.whirlpool.net.au/forum-replies.cfm?t=%#", [threadID objectAtIndex:indexPath.row]];
threadContent = [[ThreadContent alloc] init];
threadContent.urlToLoad = [NSURL URLWithString:urlString];
[self.navigationController pushViewController:threadContent animated:YES];
}
(or make the webView a property and call the load method in just before or after you push the WebViewController in Threads, I'm not 100% sure if this way would work)