HI have a custom view class that is loaded and placed within my main view using the following code. The reason it that i want to populate it with different content so rather than build a view in code each time if I create a custom class i can reuse this in a loop etc, I got this to work just fine in code, that is laying out the buttons label etc.
But rather than hand code it all I thought if i create a new User Interface View, then construct visually my text fields, labels and buttons on this view.
Then connect it to my custom class.
Bu this is where I am having an issue, how do I connect this view xib file so that it becomes visible when placed on my my code. I have assigned the custom class attribute within the xib file to my custom file, but what else am i missing?
.h File:
#import <UIKit/UIKit.h>
#interface blogView : UIView
#end
.m File:
#import "blogView.h"
#implementation blogView
- (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, 478, 220)];
if (self != nil)
{
NSLog(#"Blog View loaded");
self.backgroundColor = [UIColor yellowColor];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 400, 40)];
[titleLbl setText:#"This is the Title"];
[self addSubview:titleLbl];
}
return self;
}
#end
my xib file has the same name blogView.xib which is a View User Interface.
Within my main view controller and in the ViewDidLoad i have
blogView *blogItem = [[blogView alloc]init];
[self.view addSubview:blogItem];
When I run this is all works fine, but I would like to link to a .xib file to save time etc.
Thanks
Well having look around and trying bits of clues and suggestion I managed to do this with the following:
Within my .m file I placed/Changed the following:
self = [super init];
if (self != nil)
{
NSArray *theView = [[NSBundle mainBundle] loadNibNamed:#"blogView" owner:self options:nil];
UIView *nv = [theView objectAtIndex:0];
.. rest of code.
[self addSubview:nv];
.. rest of code.
Many Thanks
I struggled with this for an hour when I RENAMED my viewcontroller class. This is what worked for me in Xcode 5
Go to your XIB file
Click on Files owner transparent box on the left
Open up your inspections tab(Third button on right in the View Section - in between Editor and Organizer)
Go to your identity Inspector(3rd from the left) underneath the editor organizer view tab.
Fix the custom class - Class option to whatever class you want it to respond to.
Lets just say I was extremely annoyed after wasting time with that
You might want to create a controller for your view and load that view using initWithNibName:bundle:
Related
I will start off by describing what I'm trying to accomplish and then follow by describing what I've tried already. I am pretty sure what I've tried is NOT the best approach so please correct my approach as needed!
I have a ViewController.m and a Custom View which is laid out in CustomView.xib. The custom view has UIButtons and UILabels which are populated from an Array of Custom Objects.
The user flow should go as such: ViewController starts off showing CustomView with its labels populated by
CustomObjectArray[0] -> User presses button -> Another "copy" of CustomView slides into the view, over the previous version.
It's labels and buttons are populated by CustomObjectArray[1] -> User presses button -> repeat until end of Array.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So far I made a ViewController.m/h, a CustomView.m/h AND a CustomView.xib file. I used interface builder to do the layout.
On the "Custom Class" tab for the top-level View in XIB file, I type in "CustomView". I also drag IBOutlets from the XIB file to the CustomView HEADER (.h) file.
In the ViewController, under -(instancetype) init method, I create a custom view using the normal initWithNib method. And then I do:
self.view = CustomViewVariableName;
When I run the program, the view show's up fine. However, when I try to selector's, nothing's getting recognized by the buttons:
[currentCustomView.continueButton addTarget:self action:#selector(continueButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
What did I do wrong here?
More importantly, given my described goals up top, am I even doing this right? Do I need to have CustomView.m/h files? Or can I do the same thing with ONLY the XIB and the ViewController file. Remember that I need to have "multiple copies" and slide them on top of each other until the end of my custom objects array.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Some more code as requested.
In ViewController:
- (instancetype)init
{
currentCustomView = [[[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:nil] objectAtIndex:0];
self.view = currentQuizQuestionView;
.......
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[currentCustomView.continueButton addTarget:self action:#selector(continuePressed:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)continuePressed:(id)sender{
NSLog(#"Current position");
//[self moveInQuestion];
}
In CustomView.h:
#import <UIKit/UIKit.h>
#interface CustomView : UIView
#property (weak, nonatomic) IBOutlet UIButton *continueButton;
#end
In CustomView.m:
this is just the default page, I added nothing in this file.
CustomView.xib:
I'm not sure if I fully understand your requirements. Let's assume you would like to have several CustomViews as subviews of your ViewController and those subviews can be display each by each after pressing its own button.
First thing about adding subview:
self.view = CustomViewVariableName;
Since CustomsViews will be subviews according to my assumption, above line is an error. You probably would like to have something like this.
for (NSInteger i = 0; i < 5; i++) {
CustomView *v = [[CustomView alloc] initWithNib];
[v.button addTarget:self
action:#selector(continuePressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:v];
}
Second, I noticed you would like to have an array as a handler to refer all the subviews. We can add above CustomView into a mutable array by insert one more line into above for-loop.
[_customObjectArray addObject:v.button];
Finally, I created a simple project and tried to implement things you mentioned. Maybe you can take it for a reference. https://db.tt/Och2tzyG
I have a table view, which was added in Interface Builder. How can I get its instance in the view controller?
Is there a way like in Android by using getViewById()?
And if so where do I give the view its id?
I think you are starter in iPhone sdk, the solution goes as follows:
At first in the .h file of your view controller, add these lines, IBOutlet UITableview * myTableView;
Now open the .Xib file of the same view controller and then link the TableView from the xib to the files owner, there the name of the TableView will appear.
Also link the delegate and data sources of the Tableview.
Create and add UITableView programmatically and use it object (This is only for UITableView).
self.tblView = [[UITableView alloc] init];
self.tblView.frame = CGRectMake(0, 87, self.view.frame.size.width, 374);
self.tblView.delegate = self;
self.tblView.dataSource = self;
[self.view addSubview:self.tblView];
Here self.tblView is object of UITableView you can use it as per your requirement.
Or get each object of subview of UIView
NSArray *subViewArray = [self.view subviews];
for (id obj in subViewArray)
{
// here **`obj`** get each object of subView of UIView.
}
I am wanting to create a custom UIView class that will show a dynamic number of UISegmentedControl objects depending on some input. For example, if a client has 5 products in their cart, the UIView should generate 5 UISegmentedControl objects that I will then link with each item.
The problem I am having is getting this to work in a UIView. Here is what I have done so far. I am successfully able to create a UISegmentedControl object and display it programmatically within my main UIViewController. I don't get any display when adding it to my UIView class. Here is the implementation code for the UIView class:
#import "ajdSegmentView.h"
#implementation ajdSegmentView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *itemArray = [NSArray arrayWithObjects:#"Yes", #"No", nil];
UISegmentedControl *button = [[UISegmentedControl alloc] initWithItems:itemArray];
button.frame = CGRectMake(35,44, 120,44);
button.segmentedControlStyle = UISegmentedControlStylePlain;
button.selectedSegmentIndex = 1;
[self addSubview:button];
}
return self;
}
#end
I created a new UIView object via Storyboard and placed it inside the UIViewController scene. I made sure to set the class from the generic UIView class to my new custom class. I added and outlet for the UIView in my UIViewController class. Here is the code inside the implementation of UIViewController:
#import "ajdViewController.h"
#interface ajdViewController ()
#end
#implementation ajdViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.segmentView = [[ajdSegmentView alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
That's all I have tried. I have been searching through a lot of pages and trying to implement this without asking here, but I seem to be looking in the wrong places.
First you need to check ajdSegmentView is UIVIew or UIViewController. It is fine if it is UIView. If it is type of UIViewController then you need to add this line while adding Segment.
[self.view addSubview:button];
In place of:
[self addSubview:button];
And One more thing You forget to add this View to your main after allocating so You can declare like this:
objajdSegmentView = [[ajdSegmentView alloc] init];
[self.view addSubview:objajdSegmentView.view];
I have just added this thing. i got result like this way.
Hope this will work for you.
You're initializing your custom view using the init method, but your initialization for ajdSegmentView is in your initWithFrame: method (which in your case is not getting called).
So replace:
self.segmentView = [[ajdSegmentView alloc] init];
with:
// Change the frame to what you want
self.segmentView = [[ajdSegmentView alloc] initWithFrame:CGRectMake(0,0,100,40)];
Also don't forget to add your view to the view controller's view also.
[self.view addSubview:self.segmentView];
Unless this view is being created with interface builder, in which case you will need to override initWithCoder: in your ajdSegmentView class.
I'm not familiar with Storyboard though, so maybe I'm missing something, but in a standard scenario what I said above will solve your problem.
I am a new to iOS development, and I recently came across QuickDialog. From what it seems, it creates the dialog page for you automatically.
My recent learning has taught me to use the Storyboard to create the views. I was wondering, would QuickDialog integrate with Storyboard? So say I had a login form made by quick dialog, would the login view appear on the storyboard?
Thanks!
Your link seems broken. But I guess you are talking about this library?
Well, storyboard doesn't change much of the development environment. It just handles some transition between view controllers for you. So, yes, you can use QuickDialog with storyboard.
But it will not just appear in your storyboard. You need to add view controllers implemented with QuickDialog in it by yourself.
You have to create the QRootElement when storyboard is creating the controller in initWithCoder, and if you want to use the grouped option, it has to be set there, for the rest of the options, you can set them in the viewload, apart from that it's as usual:
Create a new class inheriting from QuickDialogController
Add in your class implementation the following code:
-(id) initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
QRootElement *_root = [[QRootElement alloc] init];
_root.grouped = YES;
/* Put your init code here or in viewDidLoad */
self.root = _root;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
QSection *section = [[QSection alloc] init];
QLabelElement *label = [[QLabelElement alloc] initWithTitle:#"Hello" Value:#"world!"];
[section addElement:label];
self.root.title = #"Hello World";
[self.root addSection:section];
}
Set the custom class of your storyboard UIViewController to be the one you just created
I have a UINavigationController/UITableView and I want to present a UIView over top of it when the table is empty to give the user a prompt on how to add items to the table.
I've never make a UIView (as opposed to a UIViewController before) so I'll step through what I did to make it:
Make a new UIView Class - MakeSentenceHelperView
Make a nib called MakeSentenceHelperView.xib
Set File's owner to MakeSentenceHelperView
Load the nib in the MakeSentenceHelperView:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(#"makesentencehelperview init");
[[NSBundle mainBundle] loadNibNamed:#"MakeSentenceHelperView" owner:self options:nil];
}
return self;
}
and present the MakeSentenceHelperView in the UITableViewController:
//present the placeholder view for sentences
MakeSentenceHelperView *makeSentenceHelperView = [[MakeSentenceHelperView alloc] init];
NSLog(#"present placeholder: self.navigationcontroller.view: %#", self.navigationController.view);
//Something like this:
[self.navigationController.view addSubview:makeSentenceHelperView];
[self.navigationController.view bringSubviewToFront:makeSentenceHelperView];
The class loads and logs ok, but nothing appears in front of the UITableView - where have I gone wrong?
UPDATE: if I add [self.tableView setHidden:YES]; then the tableview disappears and the space is black and empty. I'm assuming this means I'm setting up the View wrong, somewhere.
You can use https://github.com/ecstasy2/toast-notifications-ios/ for showing Toast view liek Android. Check array size and if table view is not showing then called this one and show any custom method.
Thanks to #Aadhira for their link which led me to the problem.
I needed to add awakeFromNib
I was missing [self addSubview:self.view]; at the end of initWithFrame.