I made a subclass of UIViewController.
It has a UIWebView property called *_webview.
When I set that UIViewController's view property to be _webview, I stop getting notifications from my view. So I can't implement any method with the delegate, not even viewDidLoad! I am not sure why it is happening or what am I doing wrong
Don't set the view controller's view to be the web view. In the viewDidLoad method, create the UIWebView and add it as a subview of the view controller's view. Something like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
// setup rest of webView properties such a delegate, etc.
webView.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleHeight;
[self.view addSubview:webView];
}
This will make the web view fill the view controller. This code assumes ARC. No web view property is needed unless you need to reference the web view from any methods other than the delegate methods.
Related
I have a ViewController it has 3 views. What I want to do is without loading the default view when loading the ViewController, load other view of the same ViewController (rarther than load the main view)
IS this possible. Then how I can do that?
Thanks
You'll have to call the addSubView method of the UIView class.
So, when your initial view loads in the viewDidLoad method of your UIViewController, you add another sub view to it.
[self.view addSubView : YOUR_CUSTOM_VIEW_HERE];
You have to give more precise description of your problem.As when you talk about a view, it can be a view like UIButton which can be added like
[Self.view addsubview:yourView];
But if you have created that view in some other custom class which is subclass of UIView ,then in your viewController.m
- (void)viewDidLoad
{
obj =[[customView alloc] init];
self.view =obj;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Where customView is a subclass of UIView whose view you wan't to load, not the view of the original viewController file.
What you are doing over here is that when the viewDidLoad method is called , you change the view of that viewController to view of the customView (subclass of UIView).
(adding view as addSubview is best option I think)
Even though I am setting delegate and datasource, the data source methods are never being called.
I have a ViewController that adds a subview as such:
EVPhotoCollectionViewController *pc = [[EVPhotoCollectionViewController alloc] initWithNibName:#"EVPhotoCollectionViewController" bundle:nil];
self.damagePhotosView = pc.view;
Inside EVPhotoCollectionViewController I have delegate and datasource wired up in the xib, but also via code as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView reloadData];
}
None of the datasource methods are ever called. I have verified self.collectionView is not null when it calls reloadData.
Thanks!
I think there are several things wrong here--and there are some complexities to view controller containment that you may need to read up on.
First off, you're not adding the EVPhotoCollectionViewController view as a subview of your vc, eg:
[self addSubView:pc.view];
Also, you're not setting a frame for the EVPhotoCollectionViewController, so depending on how it's implemented, it might not show up with the right size/position.
Lastly, it doesn't look like you're retaining the EVPhotoCollectionViewController anywhere. Its view will be retained by the view hierarchy, but it looks like the instance of EVPhotoCollectionViewController will be dealloc'd once the function creating it goes out of scope.
View controller containment: How does View Controller Containment work in iOS 5?
I would like to use my viewDidLoad function in my tableViewController.
How can I make viewDidLoad run in my controller?
tableViewController = [[TableViewController alloc] init];
UITableView *tableView = [[UITableView alloc] init];
tableViewController.view = tableView;
....
From Apple documentation:
This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.
So you can try to instantiate it from NIB or overwrite the loadView method. Another step from Apple documentation:
If you cannot define your views in a storyboard or a nib file, override the loadView method to manually instantiate a view hierarchy and assign it to the view property.
viewDidLoad will be called when the view is actually loaded, which will happen after you present your view controller, by, e.g.:
adding it to a navigation controller,
adding it to a tab bar controller,
presenting it modally.
This is the missing bit in your code. If you explain how you would like to present your view controller, I may help further. Also, have a look at this: Presenting View Controllers.
(I assume the fact that you tried to override the view property of your table view controller was just an attempt "to make things work" -- but you do not need to do anything about that, the view controller will be correctly set up with a table view inside of it).
tableViewController = [[TableViewController alloc] init];
tableViewController.tableView // This is your newly generated tableview
viewDidLoad will be called after you assign the tableView to another parentview
Hy
i have two classes uiviewcontroller and uiview. I have one view controller. Inside i have uiview. Inside uiview i have textfield and when i write a text and click done i need to refresh uiviewcontroller.
I tried with this in uiview class:
-(IBAction)textFieldReturn:(id)sender
{
ViewController *vc = [[ViewController alloc] init];
[vc viewDidLoad];
}
i need refresh the same as you click the button and open viewcontroller.
I am guessing you mean that you want to "refresh" the view, not the view controller. To do that simply call [self setNeedsDisplay] from the view, or [self.view setNeedsDisplay] from the view controller. Also make sure that the textfield is a subview of the uiview. Either do that in the nib file or in code by calling [self addSubview: (textfield here)].
Also, if you want to access the view controller from the view you will need to create an IBOutlet, simply allocating a new ViewController object within the view does not mean that the created view controller controls the view. Hopefully that makes sense. I'd recommend going through some ios starter tutorials as well. Just google that there are a lot.
I am not familiar with iOS UIViewController's detail implement. I have the following code to create a new UIViewController and show it, but the frame I set during initWithFrame method does not worked, the controller's view always is fullscreen(320*480).
UIViewController *viewController = [[UIViewController alloc] init];
// view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 130)];
viewController.view = view;
[view release];
AppController *app = (AppController*)[[UIApplication sharedApplication] delegate];
UINavigationController *nav = [app navController];
[nav pushViewController:viewController animated:YES];
[viewController release];
I search releative thoughts in apple developer documents, but I found nothing useful for this. How did UIViewController deal with its view frame property before show it? Where can I found useful documentation.
Thank you.
Update: In fact, the code is from cocos2d-iphone DirectorTest: https://github.com/cocos2d/cocos2d-iphone/blob/release-2.0-rc1/tests/DirectorTest.m#L143
You've got this all wrong - you really need to read (and understand) Apple's UIViewController docs:
View Controller Catalog
View Controller Programming Guide
View Controller Reference
If you're creating a view in code for a view controller, you should do it in the view controller's loadView method.
Directly from Apple's documentation:
Creating a View Programmatically
If you prefer to create views programmatically ...
you do so by overriding your view controller’s loadView
method. Your implementation of this method should do the following:
Create a root view object. The root view contains all other views
associated with your view controller. You typically define the frame
for this view to match the size of the app window, which itself should
fill the screen. However, the frame is adjusted based on how your view
controller is displayed. See “View Controller View Resizing.”
You can use a generic UIView object, a custom view you define, or any
other view that can scale to fill the screen.
Create additional subviews and add them to the root view. For each
view, you should do the following:
Create and initialize the view. For system views, you typically use
the initWithFrame: method to specify the initial size and position of
the view. Add the view to a parent view using the addSubview: method.
Assign the root view to the view property of your view controller.