I have a ViewController called Tag that has labels and photos I need to recreate on another viewcontroller called "Task" with a scrollview. I've added these by saying:
Task.m
- (void)loadFromNib {
Tag *tag = [[Tag alloc] init];
[self.view addSubview:tag.imageView3];
[scroll addSubview:tag.imageView3];
The image only loads when it's not added to the scrollview.
Related
I need to have a Searchbar inside a ViewController as the picture bellow. I don't wanna use UItableViewcontroller. Is it possible to have this searchbar that will help me to query in a NSMutableArray thanks to NSpredicate ?
My Goal
After Searching showing the results of the array in the View "white view".
Yes, It is possible.
You can create a search bar programmatically.
First,Add UISearchBarDelegate Protocol in .h (header) file.
#interface ViewController : UIViewController<UISearchBarDelegate>
Then, Create UISearchBar and add as subview in your view.
UISearchBar searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(30, 80, screen.size.width-60, 40)]; //give your frame x,y and width and height
searchBar.placeholder = #"Enter City Name";
searchBar.delegate = self;
//searchBar.hidden = NO;
[self.view addSubview:searchBar];
Now, Implement delegate methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar{
//your code when search button clicked
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
//your code when you are entering text in search bar
}
I have a TableView with a search bar. But I want to add another view (lets say a label) in to the TableView (so it scrolls with the tableview) . For an example, if you open iPhones Phone Contacts application, under All Numbers, you can see a lable called "My Number:". Which must be at the same hierarchical level as the searchbar. I tried to add a new label just above the search bar, but storyboards don't allow me to do that.
In here if I drag and drop a UILabel to this hierarchy it either replaces the searchbar at the top , or the label drops inside of the prototype cell. There is no other way of showing searchbar and Label both at once.
Is there any way of doing this?
Create a separate view outside the view controller's view (or inside, it doesn't matter. you can even create this programmatically) and link it to an IBOutlet myCustomView.
Create an IBOutlet searchBar and link your UISearchBar
in viewDidLoad; you can use
[searchBar addSubView:myCustomView];
and add your custom view above the search bar.
you can show/hide (myCustomView.hidden = YES / NO) the custom view or add and remove it from superview whenever you need.
ex.
- (IBAction)didTapShowCustomHeaderButton {
myCustomView.hidden = NO;
}
- (IBAction)didTapHideCustomHeaderButton {
myCustomView.hidden = YES;
}
add the below two delegates in the code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
label.text = #"My Label";
label.backgroundColor = [UIColor clearColor];
return label;
}
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
return 30 // this height should be equivalent to the myLabel height u declared in headerView above
}
I have an app that displays a list of products that can be purchased. The view controller for the list looks like this:
#interface InAppPurchaseListUIItemViewController :
UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *_inAppPurchaseListTable;
.
.
.
}
.
.
.
#end
This is the code that displays the list:
_inAppPurchaseListUIItemViewController = [[InAppPurchaseListUIItemViewController alloc]
initWithItemList: [[InAppPurchaseManager getInstance] getAuthorisedInAppPurchaseProducts]
: self
andSelector: #selector(inAppPurchaseSelected:)
];
_inAppPurchaseListCustomUIView= [[InAppPurchaseListCustomUIView alloc]
initWithFrame:CGRectMake(0, 0, inapp_purchase_table_width, inapp_purchase_table_height)];
[_inAppPurchaseListCustomUIView addSubview:_inAppPurchaseListUIItemViewController.view];
_inAppPurchaseListUIItemViewController.view.frame = _inAppPurchaseListCustomUIView.frame;
InAppPurchaseListCustomUIView subclasses UIView and is otherwise an empty class.
There is also an .xib with the same name as the view controller. The .xib seems to contain a Table View and nothing else.
The list displays fine, but now I want to change the items displayed. I have an NSMutableArray backing the table. When I change the array, I need to get the UITableView to redraw with the new array, but _inAppPurchaseListTable is allways nil.
How do I get a reference to the UITableView so I can call reloadData?
Is there anyway I can get rid of the .xib? I don't see the point of an extra file that does nothing but hold a table view.
Connections Inspector
I figured it out finally. The answer is on page 43 of the Table View Programming Guide for iOS.
I need to add this to the view controller:
- (void)loadView
{
tableView = [[UITableView alloc]
initWithFrame: [[UIScreen mainScreen] applicationFrame]
style: UITableViewStylePlain
];
tableView.autoresizingMask =
UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
self.view = tableView;
[tableView release];
}
Then I deleted the .xib, and the IBOutlet in the view controller.
Now I can reload the table view with [tableView reloadData];. And I don't need an .xib.
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
Hi I have a Uitableview added to a Uiviewcontroller as seen in the code below. The controller is part of a UITabbarcontroller.
The problem here is that the tab bar (at bottom of screen) overlaps with the table view. Is there any way to shorten the table height so that it is not partially hidden by the tab bar?
- (void)viewDidLoad
{
[super viewDidLoad];
self.view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
self.feedTableView = [[UITableView alloc]init];
self.feedTableView.frame = self.view.bounds;
self.feedTableView.delegate = self;
self.feedTableView.dataSource = self;
[self.view addSubview: self.feedTableView];
[self getData];
[self.feedTableView reloadData];
}
You have two problems here:
You set self.view in viewDidLoad (it should already have been set in loadView).
You set the view frame to applicationframe, but your view does not take up the whole applicationframe (the tab bar controller's view probably does that).
When you have a tabbarcontroller and set viewcontrollers inside it, the tabbarcontroller sets up all the viewcontrollers (tells them when to load and what size to have and when to show). You should never need to set the view's frames in this case. So removing that first line after [super viewDidLoad]; should fix your problem.