Adding CPTGraphHostingView as a subview - xcode

There isn't a lot of questions about this and none of them were answered in a way that could solve my problem, still sorry if I'm asking something that was asked already.
My problem is that I'd like to use core plot to draw a graph inside a view. So I add the view on IB in my appviewcontroller.xib , but thenI can't find how to link that specific zone I just defined on IB to the file's owner.
A bit of precision: I managed to use core plot in another application, when it was the main view, but now that I want to add it to an existing application, so an existing view, it doesn't work...
And to add the new zone, I just went into IB, and dragged a view from the objects library onto my appviewcontroller.xib.
So how do I link that particular view to the code I wrote for it ?
Thanks for any help !

U have to add uiview created to self.view,then add graph hosting view to it...
Try this
- (void)loadView {
self.view =view1;
}
- (void)viewDidLoad {
[super viewDidLoad];
CPGraphHostingView *hostingView = [[CPGraphHostingView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
hostingView.backgroundColor=[UIColor whiteColor];
graph = [[CPXYGraph alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
hostingView.hostedGraph = graph;
[view1 addSubview:hostingView];

Create another view(view 2) in a main view(default view let it be view)in the storyboard(xib file) and make sure it (view 2) as outlet in the header file(.h).
Drag and Connect it to the newly created view in the storyboard(xib file) so after making necessary connections in the storyboard,storyboard should be like this:
View---view (default view connected to view) and
view 2---view (new view, view 2 connected to its view).
In the loadView method of implementation file,try out these code
-(void)loadView {
[super loadView];
newView = [[CPTGraphHostingView alloc] initWithFrame: [[UIScreen mainScreen]applicationFrame]];
[self.view2 addSubview:newView];
}

Related

Change height of formSheet in iOS 8

I just have one question related to presenting a from sheet view controller in iOS 8. In iOS 7 I was able to change the height of the view controller using the last line of code in the function below:
SendRemainingEvaluationsViewController *sendRemainingEvaluationViewController = [[[SendRemainingEvaluationsViewController alloc] initWithNibName:#"SendRemainingEvaluationsViewController" bundle:nil]autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:sendRemainingEvaluationViewController] autorelease];
navigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navigationController animated:YES completion:nil];
//To Change the default size of the viewController which is presented in UIModalPresentationFormSheet presentation style
navigationController.view.superview.frame = CGRectMake(navigationController.view.superview.frame.origin.x, navigationController.view.superview.frame.origin.y, navigationController.view.superview.frame.size.width, 230);
But this does not work on iOS 8, not sure why? Does anyone knows what is the problem? any idea will be appreciated.
Thanks in advance.
After checking it, I could figure out the problem. After initializing the view navigation controller, its superview is nil and you can't access it right after presenting it. As a workaround, you can change the frame of its superview in the viewwillAppear of the view controller you're trying to present as you can see below:
- (void) viewWillAppear:(BOOL)animated
{
self.parentViewController.view.superview.frame = CGRectMake(self.parentViewController.view.superview.frame.origin.x, self.parentViewController.view.superview.frame.origin.y, self.parentViewController.view.superview.frame.size.width, 230);
}
This should work.

Xcode 4.2.1 UIScrollViews not scrolling in storyboard

So basically I have this project, where I have 4 different tab bar pages. 2 of them uses navigation controller & tab bar, and 2 are just view controllers. Now there is this one viewcontroller, which I need to add a scroll view to. So basically, I click on the item in the tab bar, it takes me to a view controller, where I can scroll down and up. I have been following Youtube Video Link, but I do not get it to work. The problem is that I use the exact code, and I change the ViewController class to the .h and .m name (ScrollViewViewController), and I put in a scroll view that has 320 x 1000, and a button at the top, but it doesn't scroll! How can I solve this problem?
Note: That if you don't know my problem, but could very detailed walk me through on how to add a scroll view to storyboards in xcode 4.2, then that would be amazing :)!
scrollerViewController.m:
- (void)viewDidLoad {
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 1000)];
[super viewDidLoad];
}
scrollerViewController.h
#interface scrollerViewController : UIViewController {
IBOutlet UIScrollView *scroller;
}
#end
And here comes a picture of the storyboard.
I need some of the code in order to really know your problem, but let me throw in my guess. You may be setting the size 320x1000 to the frame size of the scrollview by any chance? The frame size of the scrollview has to stay 320x480, and the contentSize should be 320x1000. Sorry if it was a wrong guess, but shouldn't do any harm :)
Something like this in your ViewController of the scrollView...
- (void) viewDidLoad {
[super viewDidLoad];
UIScrollView * scrollView = self.view;
scrollView.frame = (CGRect){scrollView.frame.origin, CGSizeMake(320, 480)};
scrollView.contentSize = CGSizeMake(320, 1000);
scrollView.backgroundColor = [UIColor whiteColor]; // I am setting the white background, so that the scroll indicator is visible
}
I am assuming you are setting ScrollView as the top view of the ViewController in your Storyboard as in the attached image.
I followed the exact same movie and it did not work for me either. But this movie did: http://www.youtube.com/watch?v=YBlwepYK7Zs
I followed that one and now i got it to work.
A bit late but might be more people looking for a solution to this.

Design custom UIView using XCode 4

This has been asked before, but I cannot find a definitive answer.
I would like to design a custom UIView class. I would like to do the layout in XCode 4 in a XIB file. Ideally:
I have the files MyView.h, MyView.m and MyView.xib.
The code defines the behavior and the XIB file defines the layout.
The code may have outlets into the XIB file to reference the layout elements.
The loader of the view could be different objects.
I'd like to be able to load the view by:
MyView *v = [MyView myView];
I've tried lots of different methods of setting the File's Owner and loading the XIB via NSBundle, but I keep getting key value coding problems.
Can anyone share the basic method to do this?
I have a repo of my current code here. As you can see, it generates a key value coding error.
Oh, I seem to have figured it out.
Keys are the following:
The XIB file UIView only needs to be a generic UIView. No need to set it to your subclass.
The File's Owner needs to be your subclass.
Outlets go to the File's Owner.
In your custom UIView, load the NIB as follows:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
UIView *v = [[nib instantiateWithOwner:self options:nil] lastObject];
v.frame = self.frame;
[self addSubview:v];
}
return self;
}
How exactly do you instantiate it? In my "container" class I have an outlet for my custom view as a property, but when I try to do self.customView = [[CustomView alloc] init];
the view does not come on screen, even though I know that it's correctly initialized because I can see the NSLog()s I put in my custom view's initWithFrame: method.
From within my main view controller, BEFORE instatiating the new class, the view is as I've defined it in the storyboard: <CustomView: 0x6a83bb0; frame = (38 153; 244 153); autoresize = RM+BM; layer = <CALayer: 0x6a83c60>>
However, inside the custom view's initWithFrame:, the frame is {{0, 0}, {0, 0}} and the view is <UIView: 0x68ca160; frame = (0 0; 0 0); autoresize = W+H; layer = <CALayer: 0x68d1800>>.
After the initialization has taken place, the main view's property for the custom view is barely (null), which baffles me.
I'm sure I'm missing something very simple here. Are you even supposed to define the custom view's within the main view in the storyboard at all?
Thanks!

How do I create a split view where the top panel is collapsible in XCode?

I have a UINavigationController which contains several UIViewControllers. I would like to create a shared "panel" that is displayed at the top of each view. In addition, I would like to make that panel expand and collapse, overlaying the view, when tapped.
View1
-Top Panel (collapsible)
-Main Panel
View2
-Top Panel (collapsible)
-Main Panel
This would be similar to the way the toolbar or navigation panel hide/show for the camera controller, but it would be a custom view. This is an iPhone app.
As a new XCode developer I would appreciate insights as to how this should be approached from an architectural standpoint.
Create a UIViewController subclass, say called PanelStyleUIViewController,
which would be a superclass for all the views that will have the panel. The superclass implements the panel's controller logic, and the view expansion and contraction, which always happens above the children controller's regular view logic.
This is going to be a modestly difficult project for a new iOS/cocoa developer, because:
You probably will end up having to write a lot of the folding panel's view code programmatically. It is possible to design it in interface builder using more advanced techniques but IB's basic usage is to design one view controller at a time. You need to design a partial, parent view controller that then is inherited to a number of different ones.
You need to make sure that the folding view is always above (z-index wise) the regular view content of the lower level view controller classes. This is something that is probably solved by doing a bringSubviewToFront call on the panel view on, say, the viewDidAppear method.
You are going "off road" from how standard iPhone apps behave. Whenever you do this, you make headaches for yourself and might find yourself at a dead end. My recommendation would be to stay "inside the lines" for a while until you are pretty confident with objective C, cocoa touch, etc.
That said, here's some untested code I wrote here in the stack overflow editor which should give you an idea of what I mean for this superclass design:
// PanelStyleUIViewController.h
#interface PanelStyleUIViewController : UIViewController {
UIView *panelView;
}
#property (nonatomic, retain) UIView *panelView;
// PanelStyleUIViewController.m
#implementation PanelStyleUIViewController
#synthesize panelView;
- (void)viewDidLoad {
[super viewDidLoad];
// setup geometry, contents, etc of panelView programmatically...
self.panelView = [[[UIView alloc] init] autorelease];
panelView.frame = CGRectMake(0,0,320,200);
// set resizing mask appropriately for landscape support
panelView.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleBottomMargin;
// put a button on the panel view so it can be tapped to slide down
UIButton *slidePanelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
slidePanelButton.frame = CGRectMake(0,160,320,40);
[slidePanelButton addTarget:self action:#selector(slidePanel) forControlEvents:UIControlEventTouchUpInside];
[panelView addSubview:slidePanelButton];
// set panelView with a transform offset to slide it up under the top of the screen
panelView.transform = CGAffineTransformMakeTranslation(0, -160);
// add panelView to regular view controller's view
[self.view addSubview:panelView];
}
- (void)viewWillAppear {
// make sure the panel shows up on top, z-index wise, since subclasses might have created views that overlap it.
[self.view bringSubviewToFront:panelView];
}
- (void)slidePanel {
// remove the panel transform in an animated fashion (slide down).
// TODO: some button or action needs to slide the panel back up...
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[panelView setTransform:CGAffineTransformMakeTranslation(0,0)];
[UIView commitAnimations];
}
- (void)viewDidUnload {
[super viewDidUnload];
// always make sure you clean up progammatically-retained views here
self.panelView = nil;
}
- (void)dealloc {
// and here too
self.panelView = nil;
[super dealloc];
}

Custom header and footer in an iPhone table view

I'm trying to implement something just like the HeaderFooter sample code that apple provides:
Unfortunately they used Interface Builder to do most of the work here, and I'm beginning to see that interface builder is the hardest thing to deal with in iPhone development.
My table works fine, but the header and footer aren't there at all. My question is how can I make it work.
My code is identical to Apple's, with a few name changes:
- (void)viewDidLoad
{
// headerView
CGRect newFrame = CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, self.headerView.frame.size.height);
self.headerView.backgroundColor = [UIColor clearColor];
self.headerView.frame = newFrame;
self.tableView.tableHeaderView = self.headerView; // note this will override UITableView's 'sectionHeaderHeight' property
// set up the table's footer view based on our UIView 'myFooterView' outlet
newFrame = CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, self.footerView.frame.size.height);
self.footerView.backgroundColor = [UIColor clearColor];
self.footerView.frame = newFrame;
self.tableView.tableFooterView = self.footerView; // note this will override UITableView's 'sectionFooterHeight' property
//...
The problem must be in my xib file, but the sample file has one giant nib that defines everything, all the way back to the window so I can't use that. I had to make my own.
I started with a uitableviewcontroller and nib and added header and footer views views to the nib.
I connected them to their respective IBOutlets in the table view controller subclass.
The delegate and datasource are connected and all the table cells work fine.
What did I forget? Why isn't it making the header and footer?
Follow up question:
How can I learn how to use IB? I try to avoid it, but sometimes I can't. It seems really poorly documented.
It worked when I used initWithNibNamed: to create the view. I'm not really sure why. It seems to use the .nib when I use initWithStyle:. I have to admit, I'm still fuzzy on the relationship between UIViewControllers and .nibs…

Resources