Loading Scroll Views from plists - iphone

here is the deal: For a news app, I'm loading a view inside a Scroll View for horizontal paging on each section, no problem with that, the PageControl demo helped a lot, but now I need to load a View inside another Scroll View programmatically so I can show the content of each section, the deal is that I need custom designed views already on a NIB file so each section looks different, so I would like to use a plist to load each custom view on the generic Scroll View.
Here is what I got until now:
- (void)loadScrollViewWithPage:(int)page{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// replace the placeholder if necessary
Seccion *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[Seccion alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
NSDictionary *numberItem = [self.contentList objectAtIndex:page];
//UIImage *image1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image1.jpg" ofType:nil]];
controller.sectionTitle.text = [numberItem valueForKey:Nombre];
controller.sectionView.text = [numberItem objectForKey:Vista];
controller.sectionId.text = [numberItem valueForKey:Id];
//Here is supposed to have the code to load the view inside the scroll view I tried using: [controller.sectionScrollView addSubview:controller.sectionView.text];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
Thanks a lot in advance

If all your views have it own nib, this might work:
UIView *view = [[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil] objectAtIndex:0];
so you can save the name of each nib in a plist.

Related

Uipagecontrol customization in iOS

I am trying to add different views on the pages of a uipagecontroller.Have total of three pages.That is in the first page i have to implement a map,in the second view,a simple view with some labels and the in the third view,a table.I followed the tutorial available here : http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html for implementing the page controller.How can i set the views of this controller based on the selected page.?Can anyone give some idea?I tried with this method.But it didn't give any way
- (void)loadScrollViewWithPage:(int)page {
NSLog(#"%d",page);
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scroll.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scroll addSubview:controller.view];
}
}
Add your required views to scrollView Object, and then you can manage all views by using this code:
https://github.com/SIMHAM/DDPageControl

How can I show an empty view in place of a UITableView?

I have a UITableViewController. When a certain BOOL is set, I want to show another view instead of the UITableView. How can I do this? I need to be able to bring the UITableView back also.
I implemented the following method in my Table view controller subclass to reload its background.
- (void)reloadBackground
{
if(myBool)
{
//load the view to be displayed from a nib
NSArray* nibs = [[NSBundle mainBundle] loadNibNamed:#"EmptyView" owner:self options:nil];
UIView* emptyView = [nibs objectAtIndex:0];
self.tableView.backgroundView = emptyView;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
else
{
self.tableView.backgroundView = nil;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
}
HTH,
Akshay
Instead of a UITableViewController, you can put two views inside a UIViewController:
#interface MyViewController : UIViewController <UITableViewDelegate> {
UITableView *tableView;
UIWebView *anotherView;
}
...
if (someBool) {
tableView.hidden = YES;
anotherView.hidden = NO;
} else {
tableView.hidden = NO;
anotherView.hidden = YES;
}
How about..
if(myBOOL == YES) {
ADifferentViewController * vc = (ADifferentViewController *)[[ADifferentViewController alloc]init];
[self.navigationController pushViewController:vc animated:NO];
[vc release];
}
Possibly something like:
if (myBool == YES) {
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:myView];
}
Now to get rid of it:
[myView removeFromSuperview];
Of course there are plenty of other methods...
Even: self.view = myView;
Use a UIView instead if UITableViewController.... from .h file change
UITableViewController to UIViewController Connect view to Files owner
from xib
take UITableView in a UIView... take another UIView for a blank view
and set its background color as Tableview color
If your bool if true
then show tableview
and hide blank View else
hide tableview
show blank view

simple UIButton always crashes my app on touch

i have a strange problem.
On my view i have two segmented controls and one button.
The segmented controls work perfectly but my button crashes the app when i touch it.
NSLog won't be called anymore.
PageViewController.h
paintingOptions = [[UIButton alloc] initWithFrame:CGRectMake(200,30,100,100)];
[paintingOptions setTitle:#"Ok" forState:UIControlStateNormal];
[paintingOptions setBackgroundColor:[UIColor blackColor]];
[paintingOptions addTarget:self action:#selector(showPaintingOptions:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:paintingOptions];
-(void) showPaintingOptions: (id) sender {
NSLog(#"Button pressed");
}
Any hints on how to solve that ?
Debug:
The debug output says nothing but (gdb).
Xcode shows me this line:
PDFViewController.h
- (UIView *)view {
return self.scrollView; }
0x32c37102 <+0014> ldr r1, [pc, #16] (0x32c37114 <-[UIViewController nextResponder]+32>)
But this function is in another view controller.
So the view where i have the button in is a subview of this scrollView.
Adding PageViewController to PDFViewController:
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// replace the placeholder if necessary
PageViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
//page+1 cause CGPDF indexing starts with 1
controller = [[PageViewController alloc] initWithPageNumberAndUrl:page+1: [chapter urlOnFilesystem]];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
From the crash log it looks like -(void) showPaintingOptions: (id) sender is not in your target self. Please make sure that.

How do I unload a UIViewController from a UIScrollView?

I am using pretty much the same code to load my UIViewController (called LessonScrollView) into the UIScrollView. The method is called when in the scrollViewDidScroll method
- (void)loadScrollViewWithPage:(int)page {
if (page < 0)
return;
if (page >= numPages)
return;
LessonScrollView *controller = [imageViewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[LessonScrollView alloc] initWithPage:page forLesson:[self title]];
[imageViewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (controller.view.superview == nil) {
CGRect frame = CGRectMake(0, 80, 320, 285);
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
In some circumstances I have quite a few pages, which I believe, remain in memory until the UIViewController responsible for the scrollView is released (or a memory warning occurs).
My question is, how would I release the LessonScrollView object. I would call a method called, say
- (void)unloadScrollViewWithPage:(int)page
in scrollViewDidScroll and page would be the current page - 1.
Thanks for any help you can offer.
Try this:
[controller.view removeFromSuperview];//to release the controller's view
[imageViewControllers replaceOjectAtIndex:page withObject:[NSNull null]];//to release the actual controller which should lead to it being dealocated

UIScrollView and UIViews

I want to use a UIScrollView to display multiple UIViews as the user scrolls through the UIScrollView control. I'm not worried about showing Pagination just yet.
I already managed to implement some of it, but is not working the way I want it to.
Currently:
I have 3 ViewControllers with different nib files. The root view controller is the one with the UIScrollView, and it to load the rest of the view controllers.
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
[self loadScrollViewWithPage:0];
- (void)loadScrollViewWithPage:(int)page {
if (page == 0) {
PageTwo *controller2 = [viewControllers objectAtIndex:page];
if ((NSNull *)controller2 == [NSNull null]) {
controller2 = [[PageTwo alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller2];
[controller2 release];
}
// add the controller's view to the scroll view
if (nil == controller2.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller2.view.frame = frame;
[scrollView addSubview:controller2.view];
}
}
if (page == 1) {
// replace the placeholder if necessary
NewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[NewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
That's all fine... However, what I want it to do is to load the root nib first. But I'm not sure how to go about this. I know I have to increase the number of pages to 3, but when I initialize the controllers I don't know how to tell it that page == 0 should be the current view.
Any ideas?
Update:
Sigh, I overlooked something.. Didn't notice that if don't specify a page at level 0 it just shows the current view hah!
Silly me.
I overlooked something.. Didn't notice that if don't specify a page at level 0 it just shows the current view hah!