Adding a View when onValueChange - SegmentController - iphone

I have a segment control added to the navigation bar. It works fine. When i click on a segment (say index 0) it should load UIView A for me. and when i click on segment 1 it should load UIView B for me.
I have initialized UIVIew A in my .h file. and i am accessing it in the .m file as follows;
Here i am adding a Button and a label to View A
UIButton *buttonAdded= [UIButton buttonWithType:UIButtonTypeRoundedRect];
signUpButton.frame = CGRectMake(100, 100, 300, 50);
......
[self.viewA addSubview:buttonAdded];
// similarly added a label too
[self.viewA addSubview:labelAdded];
Now i added another Button and a Label to View B as the previous code;
// i have not shown how to declare a button and label, coz its same as above
[self.viewB addSubview:buttonAddedB];
[self.viewB addSubview:labelAddedB];
EDIT: I ADDED BOTH THESE VIEWS (View A & View B) IN THE VIEWDIDLOAD METHOD.
The problem is that when i click the segments nothing appears on the screen. Can someone please help me modify the code so it will work ?

Your viewA and viewB are NULL.
You have not allocated/initialized your viewA. There needs to be code somewhere that either hooks up a view from your nib (an IBOutlet), or you need something like this:
UIView *viewA = [[UIView alloc] initWithFrame:CGRectMake(x,y,width,height)];
Then you can use viewA.

Related

presentPopoverFromRect from a button goes out of bounds

So I am using WEPopover to display a custom view controller pop up. I have a UIView in which inside it has another UIView called containerView. Inside this containerView, I have a UIButton. This is where I wanted to present my popover from. So here's what I did:
[self.popoverDialog presentPopoverFromRect:sender.frame inView:self.containerView permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
The issue is that the arrow and everything is showing from this button, but the popover goes out of self.containerView bounds. How can I make it so that the popover is displayed within the containerView bounds?
EDIT:
A picture is worth a thousand words, so here it is:
The light gray is the containerView I mentioned above. THe popover theoretically should be shown within that light gray bounds not going outside.
For the view that is contained inside the popover, go to it's view controller and set it's property contentSizeForViewInPopover. Here you can set the size so that it fits the bounds of your containerView.
OP wants to position the popover so that it only shows up in his container view. I found this bit of code in the WEPopoverController.m file
- (void)repositionPopoverFromRect:(CGRect)rect
inView:(UIView *)theView
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections {
CGRect displayArea = [self displayAreaForView:theView];
WEPopoverContainerView *containerView = (WEPopoverContainerView *)self.view;
[containerView updatePositionWithAnchorRect:rect
displayArea:displayArea
permittedArrowDirections:arrowDirections];
popoverArrowDirection = containerView.arrowDirection;
containerView.frame = [theView convertRect:containerView.frame toView:backgroundView];
}
You could possibly call this method and it might reposition your popover so that it is now inside your container viw.

Removing table subview from Superview

Hi In my app Intially i loaded a view from app delegate. Next when some button click on this root view I added this code.
-(IBAction)method
{
View1 *v1=[[View1 alloc] init];
CGRect frame = CGRectMake(0.0f, 100.0f, 320.0f, 250.0f);
v1.view.frame=frame;
hideView = [[UIView alloc] initWithFrame: CGRectMake(0,0,320,480)];
hideView.backgroundColor = [UIColor blackColor];
hideView.alpha =0.8;
[hideView addSubview:v1.view];
[self.view addSubview:hideView];
}
In above code View1 is another view controller and hide view is just a view in root view controller. Now totally for Root view controller two views are added. Now my question is i want to remove the two added subviews from Root view when i clicked on table cell. how to do this? And also when i added this sub views to root view scrolling also not possible to table view and saying bad access error how to resolve this? Or any other ways to do this.
You can set unique tag to any view (subview) to easily retrieve reference to them later.
In your case, for example, you can add following lines:
v1.tag = 10112;
hideView = 10113;
And get references to that views later you can type:
//view - is a root view of that subviews
View1 *v1 = [view viewWithTag:10112];
UIView *hideView = [view viewWithTag:10113];
// and now you are able to remove them:
[v1 removeFromSuperview];
[hideView removeFromSuperview];
If View1 is inside hideView, so you can just call [hideView removeFromSuperView]; to remove both, or just use the #Nekto solution to recognize the view you want to remove.
For the scrolling issue, it's normal that if you add a view on top of the UITableView you're going to loss the table's scrolling, you could look at hitTest method of UIView.

Convert UIViewController to UIScrollViewController

I am new to iPad developer,
I made one Registration form in my application, when i see my application in Portrait mode,
i am able to see whole form with no scrolling, but when i see same form in Landscape mode, i am not able to see part which is at bottom of page, for that a scrolling should be there to see bottom part.
:
In my .h file when i replace
#interface ReminderPage : UIViewController{
...
...
}
:UIViewController with :UIScrollView
and then when i add label in my .m file like this,
UILabel *Lastpaidlbl = [[[UILabel alloc] initWithFrame:CGRectMake(70 ,400, 130, 50)]autorelease];
Lastpaidlbl.backgroundColor = [UIColor greenColor];
Lastpaidlbl.font=[UIFont systemFontOfSize:20];
Lastpaidlbl.text = #"Lastpaid on :";
[self.view addSubview:Lastpaidlbl];
I am getting error on last line Property view not found on object of type classname.
i am unable to add label in my view.
Any help will be appreciated.
The question appears to be really asking how can all the components on the screen be placed inside a UIScrollView, rather than a UIView. Using Xcode 4.6.3, I found I could achieve this by simply:
In Interface Builder, select all the sub-views inside the main UIView.
Choose Xcode menu item "Editor | Embed In | Scroll View".
The end result was a new scroll view embedded in the existing main UIView, will all the former sub-views of the UIView now as sub-views of the UIScrollView, with the same positioning.
If you want to replace your UIViewController with a UIScrollView, you will have to go a bit of refactoring to your code. The error you get is just an example of that:
the syntax:
[self.view addSubview:Lastpaidlbl];
is correct if self is a UIViewController; since you changed it to be UIScrollView, you should now do:
[self addSubview:Lastpaidlbl];
You will have quite a few changes like this one to make to your code and will face some issues.
Another approach would be this:
instantiate a UIScrollView (not derive from it);
add your UIView (such as you have defined it) to the scroll view;
define the contentSize of the scroll view so to include the whole UIView you have.
The scroll view acts as a container for your existing view (you add your controls to the scroll view, then add the scroll view to self.view); this way, you could integrate it within your existing controller:
1. UIScrollView* scrollView = <alloc/init>
2. [self.view addSubview:scrollView]; (in your controller)
3. [scrollView addSubview:<label>]; (for all of your labels and fields).
4. scrollView.contentSize = xxx;
I think the latter approach will be much easier.
Please put all of your UIComponents to the UIScrollview and then it will start scrolling.
please look in to content size. please change it according to the orientation of device.
You're subclassing UIScrollView, so there is no self.view because already self is the view (of the scrollview). You dont need to subclass the scrollview, you can just embed your components in a ivar scrollview and set its contentSize (in your case, you have to enable the scrolling just when the device is in landscape mode). In interface builder you can embed the selected elements in one click, Editor-> Embed in-> scrollview.
First create scrollview
UIScrollView * scr=[[UIScrollView alloc] initWithFrame:CGRectMake(10, 70, 756, 1000)];
scr.backgroundColor=[UIColor clearColor];
[ self.view addSubview:scr];
second
change [self.view addSubview:Lastpaidlbl];
to
[scr addSubview:Lastpaidlbl];
third
set height depends on content
UIView *view = nil;
NSArray *subviews = [scr subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
CGRect frame = view.frame;
curXLoc += (frame.size.height);
}
// set the content size so it can be scrollable
[scr setContentSize:CGSizeMake(756, curXLoc)];
Finally
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
self.scr.frame = CGRectMake(0, 0, 703,768);
} else {
self.scr.frame = CGRectMake(0, 0, 768, 1024);
}
return YES;
}

Change to another view within a certain section

There are three sections in my UIView in the iOS application. May I know how can I only change to another view within one of the sections only?
For example:
When I press the next page button, section 2 will change to another UIView. However sections 1 and sections 3 will still remain.
Any help?
Thank you so much!
on Next Page button click function just do this
[pageSectionTwo removeFromSuperview];
[self addSubview:pageSectionTwoNewView];
"pageSectionTwo" this is the view which should be replaced to get another View in place
"pageSectionTwoNewView" this is the View which will show after you initial View in section 2 replaces
Add another UIButton, set its frame the same as UIButton in section 2, and hide it
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = btn1.frame;
btn2.hidden = YES;
[self.view addSubView:btn2]; // This will add btn2 to you view
When you are ready to change your UIButtons, do the following
btn1.hidden = YES;
btn2.hidden = NO;
That will hide the first one and show the second.

iPhone: Add background button to view when UITableView has no cells

I have a UITableViewController, when there is no data to populate the UITableView, I want to add a button, which uses an image. So, rather than the user seeing a tableview with no records, they will see an image that says, "No records have been added, Tap to add one", then they click and we create a new one.
I assumed I would just hide the UITableView, then create the button, but I never see the button. Here I am using:
if ([[fetchedResultsController sections] count] == 0) {
self.tableView.hidden = YES;
// Create button w/ image
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setImage:[UIImage imageNamed:#"no-rides.png"] forState:UIControlStateNormal];
[self.view addSubview:btn];
}
Ideas on why I would never see the button? When I show this view, it seems to have a transparent background for a second, then changes white...
I am not sure if it works but you can try this:
If your view controller is not a UITableViewController and it contains a UITableView
[self.tableView removeFromSuperview]; then [self.view addSubview];
If your view controller is a UITableViewController, you may need to consider to set the first row to contain the image and text. Then, you can handle the event: tableView:didSelectRowAtIndexPath: and if user click on the first cell, you trigger the method handle the button event
In a UITableViewController, self.view could be self.tableView in which case hiding the table would also hide the button. Try using a custom UIViewController and creating either the table or the button as a subview of self.view instead.
Alternately, when there is no data, you can create a single custom cell containing your button and use that instead of normal cells.
I believe you can add the button to the table footer, as the table footer is shown even when there are no cells. (Note, this is different to a section footer.)
By default the tableFooterView property of the UITableView is nil. So just create your button, and then do:
self.tableView.tableFooterview = btn;
For all future travelers, I simply set .userInteraction = true (Swift) and it worked like a charm. All in all:
tableView.backgroundView = constructMyViewWithButtons()
tableView.backgroundView!.userInteraction = true