How UIActivityController is presented? - iphone

I am trying to present a custom size UIViewController on top of UITableViewController without dismissing the UITableViewController but I could not. All I could do was to add the view of the UIViewController as subview.
The UIActivityController seems to be working differently when it is presented. The presenting view controller is not set to nil.
How can I present a custom size UIViewController -as the UIActivityController- without dismissing the presenting view controller ?
Thank you

Here's something you can do:
ActivityViewController *activity = [[ActivityViewController alloc]init];
[self addChildViewController:activity];
[self.view addSubview:activity.view];
Here, ActivityViewController is your custom UIViewController and inside its viewDidLoad or so, you can set its frame to the height you like. Or you can have a method which can be used to vary the height according to the number of buttons you have inside that UIView

Related

iphone development: how to put a UIView over UITableView [duplicate]

This question already has answers here:
How to add a UIView above the current UITableViewController
(21 answers)
Closed 8 years ago.
In my app I use storyboard. One of my elements in the storyboard is a UITableViewController. So it has a tableview inside of it.
My question is how can I put a UIView over this tableview. It is gonna be hidden and I want to make it visible when a tableviewcell in the tableview is pressed. Is that possible? How can I do that?
The best solution is use normal view controller (UIViewController) in StoryBoard. Your controller will need to implement two protocols (UITableViewDataSource and UITableViewDelegate) and you will need add UITablewView in the view controller's view.
In this case in interface builder you will be able to put any view in the view controller's view (can put it above table view).
Use tableHeaderView property.
Returns accessory view that is displayed above the table.
#property(nonatomic, retain) UIView *tableHeaderView
The table header view is different from a section header.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
Lets assume your view to be hidden/shown on top of table view is topView, declared as a global variable.
Declare topView in .h as
UIView *topView;
Now Lets assume that you have the UITableViewController object as tableViewController then, initialize the topView in viewDidLoad of tableViewController class
-(void)viewDidLoad
{
topView=[[UIView alloc] initWithFrame:yourNeededFrameSize];
[self.tableView addSubview:topView];//tableView is a property for UITableViewController inherited class
topView.hidden=YES;//Hide it initially for the first time.
}
assuming that you have the UITableViewDelegate methods implemented here is what you will do in didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(topView.isHidden==YES)
{
topView.hidden=NO;
}
else
{
topView.hidden=NO;
}
}
hope it helps.
you can also get view into front.
[view bringsubviewtofront];
I had a similar problem where I wanted to add a loading indicator on top of my UITableViewController. To solve this, I added my UIView as a subview of the window. That solved the problem. This is how I did it.
-(void)viewDidLoad{
[super viewDidLoad];
//get the app delegate
XYAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
//define the position of the rect based on the screen bounds
CGRect loadingViewRect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 50, 50);
//create the custom view. The custom view is a property of the VIewController
self.loadingView = [[XYLoadingView alloc] initWithFrame:loadingViewRect];
//use the delegate's window object to add the custom view on top of the view controller
[delegate.window addSubview: loadingView];
}

iOS4 - custom uiview accessing UINavigation stack

Bit confused with this one so bear with me...
I have a Navigation-based project which is working fine. I'm trying to create my first custom UIView to make a couple of buttons which I will use in multiple places. One of the buttons needs to push a viewcontroller into the navigation when it's clicked but I'm not sure how to do this.
When I had the button set up within a view controller I was using:
LocationViewController *controller = [[LocationViewController alloc] initWithNibName:#"LocationViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
but the self.navigation controller won't work now, will it? How do I access the navigation controller of the viewcontroller that this uiview will be added to?
Hope at least some of that makes sense, as I said it's my first go at subclassing the uiview and adding it to multiple pages so I'm a bit lost.
EDIT TO ADD - I have the button click events inside the custom UIView, so that is where I'm trying to change the viewcontroller from. Should I instead wire up the events in whichever viewcontroller I add the view to?
Usually your appDelegate has a UINavigationController property. You can access it in your custom view like this:
UINavigationController *navController = (MyAppDelegate *)[[[UIApplication sharedApplication]
delegate] navigationController];
But more effective way is to make delegate method for your custom view and handle button action in your viewController.
MyCustomView.h
#protocol MyCustomViewDelegate
#interface MyCustomView : UIView {
id<MyCustomViewDelegate> cvDelegate; }
#property(nonatomic, assign) id<MyCustomViewDelegate> cvDelegate;
#protocol MyCustomViewDelegate #optional
-(void)didClickInCustomView:(MyCustomViewDelegate*)view withData:(NSObject*)data;
#end
MyCustomView.m
- (void)myButtonClick:(id)sender
{
[self.cvDelegate didClickInCustomView:self withData:someData];
}
So now you can handle this event in any place where is your custom
view.
Add the button from the interface builder or from the view controller's viewDidLoad using code:
CGRect frame = CGRectMake(0, 0, 24, 24);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button addTarget:self action:#selector(handleMyButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Then implement -(void)handleMyButton:(id)sender {}; in your view controller. Or you could instead write -(IBAction)handleMyButton:(id)sender {}; and link method and button using the interface builder.
Then inside the method just paste the block of code you posted above. If you started with the Xcode navigation controller template project it should work.
I think it's cleaner to hide the designated initializer initWithNibName: because it is an implementation detail.
When you say you are subclassing the UIView I don't know exactly what you mean. If you want to add another view controller with a custom view just use the UIViewController template and customize the XIB file, no need to subclass an UIView unless you are really modifying its behaviour, which I guess you are not. The view is a view, and the controller stuff like handling buttons should be in the controller.
The actual controller need to be in the navigation controller stack to be able to push another controller.
Or you can make a new navigation controller instance and push your LocationViewController.

how to access superviewcontroller from its subviewcontroller in iphone

I have a viewcontroller and i am adding a subviewcontroller in that. Now i want to set value for a string from subviewcontroller which is in its superviewcontroller.
Please Suggest some idea
Thank You
if you want to access superview use the statement said by Jhaliya,
If you want to access superviewcontroller property frm subviewcontrller property ,
use
id mainViewController = [self.view.superview nextResponder];
here mainViewController is the superviewcontroller reference.
There a parentViewController property in UIViewController class reference,
This property is used for navigation, tab bar, and modal view controller hierarchies.
The above syntax is used for when you are setting a view as subview to a parentviewcontroller
You can access the super view controller using the parentViewController property on every UIViewController.
UIViewController *parent = self.parentViewController;
You can use superView method of UIView to access its parrent.
UIView *mySuperView = [MySubView superView];
use superview property of UIView.
UIMyView* mySuperView = (UIMyView*) myView.superview ;
mySuperView.mytextString = #"Assign string to super view";
EDITED:
You could get the list of all your UIViewController by using methods of UINaigationController .
Also current visible controller and top view controller,
Read the documentation for UINavigationController .

Adding a view controller's view as a subview changes it's frame?

SomeViewController *someVC = [[SomeViewController alloc] initWithNibName:#"SomeViewController" bundle:nil];
self.someViewController = someVC;
[someVC release];
NSLog(#"height before added as subview: %f", self.someViewController.view.frame.size.height);
[self.containerView addSubview:self.someViewController.view];
NSLog(#"height AFTER added as subview: %f", self.someViewController.view.frame.size.height);
// height before added as subview: 480.000000
// height AFTER added as subview: 540.000000
What possible explanation is there for this? someViewController's view does not have any autoresizing properties set, and containerView does not autoresize it's subviews. What could cause the frame to change by just adding it as a subview?
A further mystery to this is that it only happens when someViewController's view height is >= 480.
What kind of view controller is SomeViewController ? The one thing I think that may be happening is SomeViewController may contain some extra content such as the top bar of an UINavigationController and that is automatically increasing the size sometimes. But what you are doing is violating the HIG by adding the view of one view controller to another view controller. An UIViewController is designed to be the full view of the screen or belonging to a specialized view controller such as an UITabBarController, UINavigationController, or UISplitViewController. As a result someViewController will not function as expected and not receive calls such as viewDidLoad. So if you are sure that no autoresizing properties are set all I can recommend is using an UIView instead of an UIViewController.
This can be caused by the autoresizingMask of the view, it's purpose is to adjust the size to changes in it's super view.

UIScrollView with multiple UIViewControllers

i have one scrollview and 4 UIviewcontrollers with xib files now i want to add 4 viewcontrollers to scrollview
and also the scroll is enabled for four viewcontrollers
any one know this plz answer this problem
Just add them. What's the problem?
// this loads a view controller from a nib
controller = [[UIViewController alloc] initWithNibName:#"YourNibsName" bundle:nil];
// this adds the viewcontroller's view to the scrollview
[scrollView addSubview:controller.view];
// place the subview somewhere in the scrollview
CGRect frame = controller.view.frame;
frame.origin.x = 600;
controller.view.frame = frame;
// don't forget to release the viewcontroller somewhere in your dealloc
[controller release];
Do this for all your four controllers.
yes, you can just add the views of the viewcontrollers to your scrolling view, but remember that you are rolling your own equivalent to a UITabBarController or UINavigationController and so you have some responsibilities:
When you alloc init your vc from a nib the vc will get its viewDidLoad method called.
But when you put the vc.view into your scrollview YOU need to call [vc viewDidAppear:YES] (and also call viewWillAppear just before if your vc uses it).
Be careful with things like presenting modal view controllers from your vc as this may not work as you expect.
Peter
I realize that it's quite an old question. But now you can use UIPageViewController.