Xcode UIView and subviews - objective-c

first of all, I am beginner with xcode and programming in Objective-c. I made my app with navigation bar and I have UIView with class Lesson1 and I added new subview Level1 but I dont wanna add new class. Is it any solution how add label to subview Level1 from class Lesson1.m?
Thank you

It is very easy to do so. Let's admit this is your Lesson1 view code. In the viewDidLoad method you can add whatever you want.
- (void)viewDidLoad
{
UIView *level1 = [[UIView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[self.view addSubview:level1]
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(label_x,label_y,label_width,label_height)];
label1.text = #"labeltext";
[level1 addSubview:label1]
}

Yes, you can.
Programmatically:
// viewDidLoad method from Lesson1 class
- (void)viewDidLoad
{
self.level1 = // Your UIView
[self.view addSubview:self.level1]
[self.level1 addSubview:yourLabel]
}
Using XIB file for Lesson1:
Drag and drop an UILabel into your Lesson1 and link the UILabel with an IBoutlet on your Lesson1 class.

Use below link. It might be helpful for you!!
http://www.techotopia.com/index.php/An_iOS_7_Core_Data_Tutorial

Related

In UIViewController's code, [self.subViewGrid setNeedsDisplay] not calling -drawRect

I have an iPad app, using Storyboards, XCode 4.6 and iOS 6.1. I have a scene that contains a UIViewController. Inside that UIViewController, I have a UIScrollController, all created using IB. Programmatically, in viewDidLoad I created two (2) UIViews (one called subViewGrid, the other called subViewData) and added them to the UIViewController; they both display correctly in the Simulator. Here's the code:
- (void)viewDidLoad {
[super viewDidLoad];
// notify me when calendar has been tapped and CFGregorianDate has been updated
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(calendarTapNotification:)
name:#"calendarDateSelected" object:nil ];
// UIScrollVIew settings
CGSize scrollableSize = CGSizeMake(760, 1379); // set size of scheduleView
[self.schedScrollView setContentSize:scrollableSize];
self.schedScrollView.contentInset = UIEdgeInsetsMake(0,0,44,44); // allow for scroll bar
self.schedScrollView.directionalLockEnabled = YES; // prevents diagonal scrolling
// create a sub-view to hold the appointment GRID
CGRect frame = CGRectMake(0,0,760,1390); // 110,48,760,1390
subViewGrid = [[UIView alloc] initWithFrame:frame];
subViewGrid.tag = 12; // use tag to get correct sub-view
subViewGrid.backgroundColor = [UIColor grayColor];
subViewGrid.alpha = 1.0; // make it opaque
[self.schedScrollView addSubview:subViewGrid];
// create a sub-view to hold the appointment DATA
frame = CGRectMake(110,48,670,750);
subViewData = [[UIView alloc] initWithFrame:frame];
subViewData.tag = 22; // use tag to get correct sub-view
subViewData.backgroundColor = [UIColor redColor];
subViewData.alpha = 0.2; // make it sort of transparent
[self.schedScrollView addSubview:subViewData];
[self.subViewGrid setNeedsDisplay]; // **** UPDATED ****
}
Here is the .h file contents for the UIViewController:
#interface CalendarViewController : UIViewController {
UIView *subViewGrid;
UIView *subViewData;
}
#property (strong, nonatomic) IBOutlet UIScrollView *schedScrollView;
- (void) calendarTapNotification:(NSNotification *) notification;
-(NSDate *)beginningOfDay:(NSDate *)date;
-(NSDate *)endOfDay:(NSDate *)date;
#end
In my drawRect method, I have some code that is supposed to draw a "grid" on the subViewGrid. The problem is drawRect never gets called.`
I have read the UIView Programmer's Guide and looked in SO and did a Google search, but found nothing that addresses the issue, which is: why won't [self.subViewGrid setNeedsDisplay] call drawRect from where I have it placed?
Your view controller needs to call setNeedsDisplay for the view it controls, not for itself. So, you want
[self.subViewGrid setNeedsDisplay]
This is just an error in your reading the documentation. Understanding the documentation is critical for objective-C programming so I'll try to help you get a grasp of it.
If you look at the documentation for setNeedsDisplay you will see that it is either a CALayer or UIView class method. If you then look at inheritance, you will see that UIView is UIResponder:NSObject and CALayer is NSObject. None of these inherit from UIViewController which is why you are getting the error. You need to call [self.subViewGrid setNeedsDisplay]

Multiple Views in Xcode 4.2

I'm having a lot of trouble finding a tutorial for implementing multiple views in Xcode 4.2 without storyboard, this is for a class so I can't use storyboard yet. I'm just trying to have a 2nd view with a UIPicker come up when a button is clicked in the main view, I just can't find one for this version of Xcode and it's different enough from the older versions to confuse me.
Any help appreciated if someone can give me a quick description of what I need to do this or a newer tutorial I'd appreciate it :)
I think you should read the UIView Programming Guide to get a good handle on how UIViews work exactly. I find nibs/storyboard are really great at confusing new iOS developers.
In essence, a UIViewController has 1 view which you set in the viewDidLoad or loadView method by using the [self setView:someUIView]. You add more stuff to the screen by adding UIViews as a subview of the viewcontroller's "Main" view. For example
-(void)loadView {
// Create a view for you view controller
UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self setView:mainView];
// Now we have a view taking up the whole screen and we can add stuff to it
// Let's try a button, a UIButton is a subview of UIView
UIButton *newButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// We need to set a frame for any view we add so that we specify where it should
// be located and how big it should be!
[newButton setFrame:CGRectMake(x,y,width,height)];
// Now let's add it to our view controller's view
[self.view addSubview:newButton];
// You can do the same with any UIView subclasses you make!
MyView *myView = [[MyView alloc] init];
[myView setFrame:CGRectMake(x,y,width,height)];
[self.view addSubview:myView];
}
Now here we have our viewController who'se view is just a plain UIView which in turn has 2 subviews; newButton and myView. Since we created the MyView class, maybe it contains subviews as well! Let's take a look at what a UIView subclass could look like:
// Here is the init method for our UIView subclass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Let's add a button to our view
UIButton *newButton2 = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// Of course, the frame is in reference to this view
[newButton2 setFrame:CGRectMake(x,y,width,height)];
// We add just using self NOT self.view because here, we are the view!
[self addSubview:newButton2];
}
return self;
}
So in this example we would have a view controller who'se view now contains 2 button! But the view structure is a tree:
mainView
/ \
newButton myView
\
newButton2
Let me know if you have any other questions!
Matt

Add a UITableViewControler to a UIView

I am writing an appcelerator module, which means I am handed a subclassed UIView to work with and create my visual controls in Objective C.
I am trying to add a tableview with a searchbar, but most samples online use rootViewController and UITableViewControler.
so...in order to add a tableview to the current view, do I need to create a tableview and a UITableViewController and add them somehow as subviews to the current view ?
I tried adding a MainViewController.h & MainViewController.m which is defined as
#interface MainViewController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
and then in my view
#import "MainViewController.h"
- (void)viewDidLoad
{
mainView = [[MainViewController alloc] init];
[self addSubview:mainView.view];
}
-(void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
{
if(CGRectIsEmpty(self.frame))
{
self.frame = bounds;
[self addSubview:mainView.view];
}
}
but it did not work, I just got an empty view. any ideas ? a sample code would be greatly appreciated
thanks
You could try something like this:
- (void)viewDidLoad {
self.view.backgroundColor=[UIColor whiteColor];
UITableView *TableListView=[[UITableView alloc] initWithFrame:CGRectMake(-5,-1,331,425) style:1];
TableListView.editing=NO;
TableListView.delegate=self;
TableListView.dataSource=self;
TableListView.separatorColor=[UIColor colorWithRed:0.000000 green:0.591928 blue:1.000000 alpha:1.000000];
TableListView.separatorStyle=1;
TableListView.rowHeight=40;
TableListView.tag=0;
TableListView.backgroundColor=[UIColor groupTableViewBackgroundColor];
TableListView.clipsToBounds=YES;
[self.view addSubview:TableListView];
[TableListView release];
}
Hope this helps get you started...

Resizing UINavigationController?

I want my navigationcontroller to only take half of the screen. Is this possible? In IB, when I drag, it forces me to fill up my entire screen, I can't resize it. If it's not possible, is there an alternative?
Thanks.
You can but you can only do it within iOS 5 because when I tried to do any type of direct view manipulation within a UINavigation controller than pushed or popped another view controller. The navigation controller would not display them.
Here's what you need to do.
-(void)viewDidLoad {
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] initWithNibName:nil bundle:nil]];
//Used to recieve callbacks (like shouldAutorotateToInterfaceOrientation:)
[self addChildViewController:navController];
//View manipulation
navController.view.frame = CGRectInset(navController.view.frame, 20, 20);
[self.view addSubview:navController.view];
//Calls all the standard methods (viewDidLoad,viewDidUnload,etc.)
[navController didMoveToParentViewController:self];
}
Also if your a registered developer there is a really good video on view controller containers, that might be helpful to you, from last years WWDC.
Based off what you have written I am assuming you have a #property in your header file. Something like:
#property (nonatomic, strong) IBOutlet UINavigationController *navController;
Then have this IBOutlet connected to your navigationController in your xib file. In that same xib file have a view that is connected to the file owner. Set this view's dimensions in interface builder so in your case to fill only half the screen. Then you will add the navController as this view's subview. If you also have a viewController already added in the xib file in the navController then in viewDidLoad do:
-(void)viewDidLoad {
[self addChildViewController:self.navController];
[self.view addSubview:self.navController.view];
[self.navController didMoveToParentViewController:self];
}
Otherwise if you are programmatically setting the rootviewcontroller do:
-(void)viewDidLoad {
self.navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
[self addChildViewController:self.navController];
[self.view addSubview:self.navController.view];
[self.navController didMoveToParentViewController:self];
}
You have to add self.navController as a childViewController so that it will take on the dimensions of the view it is being added into. Adding it as a subview is not enough to make the view resize. Hope this helps!

Working on custom component: subclass UIView or UIViewController?

I'm working on a custom implementation of UISegmentedControl.
I'd like to create a component that able to receive config data and from which obtain a custom View similar to UISegmentedControl.
I started subclassing a UIView and i can create a custom UISegmentedControl with this code:
CustomSegment *segment = [[CustomSegment alloc]
initWithTitles:[NSArray arrayWithObjects:#"one",#"two",nil]];
[self.window addSubview:segment];
But now i'd like to improve my class and add some more customizable parameters to it.
For example i'd like add a custom separators, define the button fonts and so on... here my doubt:
Is it better to work on a UIView subClass or you suggest me to subclass a UIViewController, where i can manage View hierarchy in method like -(void)loadView and -(void)viewDidLoad ?
In a simple UIView subclass, when i launch the custom init method, i setup immediately subviews... while using a UIViewController i can call custom init and define how my subview is builded into -(void)loadView.
Don't use an UIViewController, just extend the UIView class like you did and keep extending its functionality.
Remember to save a pointer to each subview you add (i.e. buttons) in order to be able to access them later.
Define custom setters, for example, a custom setter for changing a button label title would be:
- (void) setButton1Title:(NSString*)str forState:(UIControlState)state{
//You can add some control here
if ([str length] > 20) return;
[_button1 setTitle:str forState:state]; //_button1 is my reference to the button
}
And so on. Don't provide direct access to your subviews, use methods instead.
Also, you can use "layoutSubviews" method to define how your views are going to be displayed in your custom view.
Hope it helps you.
Edit: In your case, I don't see why using lauoutSubviews method but I want to show you what I was trying to say.
Lets say that for example I need to create an UIView class to represent a "Contact" object in my application.
This is what I would do:
#interface ContactView : UIView{
UILabel* _nameLabel;
UILabel* _ageLabel;
Contact* _contact;
}
#property (retain) Contact* contact;
#end
#implementation ContactView
#synthetize contact = _contact;
-(id)initWithContact:(Contact*)c{
self = [super init];
if (self) {
_nameLabel = [[UILabel alloc] init];
_nameLabel.frame = CGRectZero;
[self addSubview:_nameLabel];
[_nameLabel release];
_ageLabel = [[UILabel alloc] init];
_ageLabel.frame = CGRectZero;
[self addSubview:_ageLabel];
[_ageLabel release];
self.contact = c;
}
}
- (void) layoutSubviews{
[super layoutSubviews];
_nameLabel.frame = CGRectMake(0.0f, 0.0f, 200.0f, 25.0f);
_ageLabel.frame = CGRectMake(0.0f, 25.0f, 200.0f, 25.0f);
if (self.contact){
_nameLabel.text = self.contact.name;
_ageLabel.text = self.contact.age;
}else{
_nameLabel.text = #"Unavailable";
_ageLabel.text = #"Unavailable";
}
}
- (void) setContact:(Contact*)c{
self.contact = c;
[self layoutSubviews];
}
#end
Check out how the "layoutSubiews" is used to set the correct frame and data to the labels.
Usually, I use it a lot when creating custom UITableViewCells where you have to reuse the view.
Let me know if I'm being confusing.