I am making a presentations app with that contains 50 slides. Each slide can contain buttons,videos,...
So I was thinking about doing it this way. I create 50 classes that are subclasses from UIView with each their own xib file. With this I can put different buttons on the UIView and use delegate methods to handle them.
Then I have one MainViewController with an header and footer image and in the middle one scrollView. I load up the XIB files like this.
UIView *rootView = [[[NSBundle mainBundle] loadNibNamed:#"page1" owner:self options:nil] objectAtIndex:0];
Put all these UIViews in an NSArray and next place them inside a scrollView by looping the NSArray
for (int i = 0; i < arrViews.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *view = [arrViews objectAtIndex:i];
[view setFrame:frame];
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * arrViews.count,scrollView.frame.size.height);
So I have the following classes:
Page1.h (subclassed from UIVIew)
Page1.m
MainViewController.h (subclassed from UIViewController)
MainViewController.m
What my question is now, how can I say that the UIViews that I created (UIView *rootView) should use the classes of Page1.
In the storyboard you have to change a Class to a custom class. So for example in your page1 XIB you have to go to Identity Inspector (3th tab) and in Class text box type Page1, and for next page you have to do it again. In that case you can call rootView like:
Page1 *rootView = (Page1*)[[[NSBundle mainBundle] loadNibNamed:#"page1" owner:self options:nil] objectAtIndex:0];
Hope this help.
Related
I am making an application, and I started with a single view controller with one view inside. I made my app but I want to have multiple pages and allow the user to swipe between them. I want to use a UISCrollView. How can I set up a UIScrollView so that I can insert my already made view from the xib. I don't want to make a view programmatically because a lot of it is graphical and I don't really know what I'm doing.
Recap: UIScrollView to swipe between views I have made in xib files
in your main view controller, add a scrollview. You can add scrollview to mainview in Xib.
In mainview controller .h file add a property , then load programmatically your views and add to scroll. Set each view position. Set scrollview reference in your main xib properly.
#property(nonatomic, weak) IBoutlet UIScrollView *scrollView;
UIView *view1 = [[NSBundle mainBundle] loadNibNamed:#"myViewXib1" owner:self options:nil];
UIView *view2 = [[NSBundle mainBundle] loadNibNamed:#"myViewXib2" owner:self options:nil];
UIView *view3 = [[NSBundle mainBundle] loadNibNamed:#"myViewXib3" owner:self options:nil];
CGRect frame;
frame = view1.frame;
frame.origin.x = 0;
view1.frame = frame;
frame = view2.frame;
frame.origin.x = view1.frame.size.width + view1.frame.origin.x ;
view2.frame = frame;
frame = view3.frame;
frame.origin.x = view2.frame.size.width + view2.frame.origin.x;
view3.frame = frame;
[scrolView addSubView:view1];
[scrolView addSubView:view2];
[scrolView addSubView:view3];
Declare IBOutlet in your view controller to point to that view.
#property(nonatomic, weak) IBoutlet UIView *myView;
Connect your view from xib to myView.
Load your xib in the view controller viewDidLoad method
[[NSBundle mainBundle] loadNibNamed:#"myViewXib" owner:self options:nil];
Then add myView to your scrollview.
I have a view created in xib with UIImageviews and UILabel as the subviews. From my code, is it possible to create a new view object every time i run a loop.Please help me.
You need to create a new custom view named CustomView
Select class in XIB to CustomView.
Then load XIB name to a View object and can use in a loop
CustomView *aView = [[[NSBundle mainBundle] loadNibNamed:#"YourXibName" owner:nil options:nil] objectAtIndex:0];
When you have created a view and hooked it up via interface builder you can easily add views like
for(int i = 0; i < numberOfIterations; i++){
UIView * newView = [[UIView alloc] initWithFrame:MyFrame];
[myInterfaceBuilderView addSubview:newView];
}
I am trying to use the CollapseClick table control. This all works, but I can't seem to add a button event to the view that is shown when the table cell is expanded.
Looking through the code, it is UITableView, which created UITableViewCells and adds UIViews dynamically. I think the UIView is added here:
+ (CollapseClickCell *)newCollapseClickCellWithTitle:(NSString *)title index:(int)index content:(UIView *)content {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"CollapseClickCell" owner:nil options:nil];
CollapseClickCell *cell = [[CollapseClickCell alloc] initWithFrame:CGRectMake(0, 0, 320, kCCHeaderHeight)];
cell = [views objectAtIndex:0];
// Initialization Here
cell.TitleLabel.text = title;
cell.index = index;
cell.TitleButton.tag = index;
cell.ContentView.frame = CGRectMake(cell.ContentView.frame.origin.x, cell.ContentView.frame.origin.y, cell.ContentView.frame.size.width, content.frame.size.height);
[cell.ContentView addSubview:content];
return cell;
}
The button displays, but if I try and hook up any events I cannot capture them.
It's probably quite simple. I have put the app here: http://www.havoc-media.com/TravelApp.zip
Upon looking at your code, you have a few overlooks, when it comes to using views.
The most important thing to know about views are that in order to have click events register on subviews the parent view cannot be smaller. The silly thing here is that even though you can visually see a view you inject into your ContentView, the Content view's width is set to zero, which means that all the views inside the ContentView may show, but will not respond to click events and the click events will default to the parentview of the ContentView, thus making your buttons unusable and unresponsive.
To address this in your openCollapseClickCellAtIndex you need to add the code:
cell.ContentView.frame = CGRectMake(cell.ContentView.frame.origin.x, cell.ContentView.frame.origin.y, 320, cell.ContentView.frame.size.height);
Your second issue is your CollapseClickCell xib you created is not communicating back to the view controller. Which means the view you created in the view controller and injected in the CollapseClickCell xib of which you want to report back to the view controller....isn't.
There is a way to address this but in my opinion I would create another custom xib-A to inject into your CollapseClickCell, and have the xib-A handle your button click events instead of test1View. Create a xib called "testView" linked to a test class and put this code in
-(UIView *)viewForCollapseClickContentViewAtIndex:(int)index
{
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"testView" owner:nil options:nil];
UIView *content = [views objectAtIndex:0];
switch (index) {
case 0:
return content; /*test1View;*/
break;
Once these two issues are addressed, your code will work. I can include the additions I made to your code to make it work if you would like to see it?
First of all, this code is not correct:
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"CollapseClickCell" owner:nil options:nil];
CollapseClickCell *cell = [[CollapseClickCell alloc] initWithFrame:CGRectMake(0, 0, 320, kCCHeaderHeight)];
cell = [views objectAtIndex:0];
You are first creating a cell via [collapseClickCell alloc]. But after that you assign the first object of the array created with loadNibNamed:owner:options:nil to the same variable. You either load the cell from the nib or you create it programatically. Simply put, you should just use:
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"CollapseClickCell" owner:nil options:nil];
CollapseClickCell *cell = [views objectAtIndex:0];
Be aware that the rect you were providing to the initWithFrame: method was not being used at all
Adding buttons to a cell is not the most appropriate thing (the whole cell should be the button). But if you absolutely need to, try assigning a uibutton as the accessory view of the cell. Take a look at http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/accessoryView
Something similar to:
UITableViewCell *cell = (obtain your cell)
UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(x,y,width,height);
cell.accessoryView = button;
I have 2 questions actually. First I'll explain the scenario. I have a simple Storyboard app. The first controller is a UITableViewController displaying a list of mail types. When tapped upon a cell, it segues to a UIViewController and depending on the selected mail type, it loads a UIView into the UIViewController. This is all working fine. My problems are,
One UIView I have is 559 in height. I put a UIScrollView inside the UIViewController so that when the UIView with the extra height is loaded, it'll be scrollable. But instead of that happening, the UIView resizes to the size of the UIViewController. How can I keep its original height and embed it inside a UIScrollView?
From the Interface builder, I've put a UIToolbar at the bottom of the UIViewController. But when the UIView is loaded into it, the UIToolbar disappears. I guess the UIView appears on top of the UIToolbar(?). How can I avoid this from happening?
I know this must a bit confusing to understand. Therefore I've uploaded an Xcode project demonstrating the 2 issues here.
Please have a look and tell me what I should do.
Thank you.
I see a couple problems with what you're doing in your project that may explain why you're getting these results.
In NewMailViewController.m, you are replacing the view you've created in the storyboard (the one that has a scroll view and a toolbar) with the one you've loaded from the nib (i.e. self.view = [nib objectAtIndex:0];). Try doing this in -loadView
[super loadView];
UIView *someView = nil;
if ([self.mailTypeName isEqualToString:#"Email"]) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Email" owner:self options:nil];
someView = [nib objectAtIndex:0];
}
else if ([self.mailTypeName isEqualToString:#"Facsimile"]) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Facsimile" owner:self options:nil];
someView = [nib objectAtIndex:0];
}
else if ([self.mailTypeName isEqualToString:#"Memorandum"]) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Memorandum" owner:self options:nil];
someView = [nib objectAtIndex:0];
}
else if ([self.mailTypeName isEqualToString:#"ProjectMemo"]) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProjectMemo" owner:self options:nil];
someView = [nib objectAtIndex:0];
}
CGRect frame = someView.frame;
frame.size.width = self.myScrollView.frame.size.width;
[someView setFrame: frame];
[self.myScrollView addSubview: someView];
[self.myScrollView setContentSize: someView.bounds.size];
You'll notice that I have a reference to the scroll view in the above snippet of code. You'll want to create an outlet for the scroll view in your NewMailViewController class so that you can refer to it as needed. It involves putting this in your header file:
#property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;
You'll need to connect the defined outlet to the actual storyboard's scroll view. You can search for "creating or connecting outlets in Xcode" on the Internet for more information about that.
The toolbar appears to be a subview of the scroll view. Don't you want the toolbar to rest at the bottom of the view while the content of the scroll view scrolls? If so, you'll want to move that toolbar view out from within the scroll view.
Here are the changes: http://www62.zippyshare.com/v/90505016/file.html
First, I have a exist Asset.m/Asset.h, it is the subclass of UIView. Then I create a empty xib file and change xib's file's owner class to Asset. Then I create a view and a label to this xib, and I can drag and create IBOutlet in UIView.
IBOutlet UIView *videoOverlay;
IBOutlet UILabel *videoTimeLable;
Then I want to add videoOverlay to Asset.m's view using addSubview.
self = [super initWithFrame:CGRectMake(0, 0, 0, 0)];
[self addSubview:videoOverlay];
But the view -- videoOverlay do not show up.
I print out size of videoOverlay, all 0:
NSLog(#"width =%f height= %f", videoOverlay.frame.size.width, videoOverlay.frame.size.height);
Why ?
Finally, I find the solution by myself. Use under code to add this view (videoOverlay and videoTimeLable belong to this view )in:
UIView *thumbs;
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ELCAsset" owner:self options:nil];
thumbs = [nib objectAtIndex:0];
[self addSubview:thumbs];
then videoOverlay and videoTimeLable can show up.