Adding custom UIView to UIViewController in - (void)loadView (Objective-C) - uiviewcontroller

I am taking a programming class (for noobs) and I need to create the UIViewController graphViewController's view programmatically (without interface builder).
The view is simple, it only consists of an IBOUtlet which is an instance of a UIView subclass called GraphView. graphView responds to several multitouch gestures for zooming and panning and what-not, but I handle all of that stuff in - (void)viewDidLoad.
I am doing just the creation of the self.view property and graphView in the code below:
- (void)loadView
{
UIView *gvcView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = gvcView;
[gvcView release];
GraphView *aGraph = [[GraphView alloc] initWithFrame:self.view.bounds];
self.graphView = aGraph;
[aGraph release];
}
When I run the app I do not see the graphView view in there. I just get a transparent view which shows the "My Universal App" label. I'm stumped. Please help.
Let me know if you need additional code.
Thanks!
Update: Big thanks to BJ Homer for the quick fix!
had to do the following:
add this line of code: [self.view addSubview:self.graphView]; at the end.
I was also getting this strange bug where graphView was showing up as completely black. This line of code fixed that: self.graphView.backgroundColor = [UIColor whiteColor];
And that's it!
Final question: Is the default background color black for a custom UIView?
Thanks again!

[self.view addSubview:self.graphView];
Until you add your graph view to a parent view, UIKit doesn't know where to display it. self.view is special, since that's the property that -loadView is supposed to set. That view will automatically be added to the screen. But your graph view is just floating off in the ether until you add it somewhere.

Related

SciChart - SCICursorModifier

For SCICursorModifier,
1. Is there any way to activate the modifier with long press gesture instead of pan gesture?
2. Is it possible to share SCICursorModifier for multi-surface? I have tried the following code as other modifiers:
cursor = [[SCIMultiSurfaceModifier alloc] initWithModifierType:[MHSCICursorModifier class]];
and then add the modifier to the surface
SCICursorModifier *cursor = [m_oCursor modifierForSurface:surface];
However, the crosshairs only appear on one surface but not the others. Please advice.

Coding a custom navigationItem.rightBarButtonItem once and having it reused in both UITableViewControllers and UIViewControllers?

I have successfully added a rightBarButtonItem to my navigationBar, but I would prefer to only have the code to do so show up once rather than once per type of ViewController. Here's my current setup:
-->TVC
|
NVC--->TVC--->TVC--->VC
So far I've subclassed UITableViewController and moved my code for adding the button into my subclass. All 3 of my TableViewControllers are set to that subclass and it works perfectly.
However now I need my lone ViewController to also show the button, but I don't know how to accomplish this without duplicating the code from my TVC subclass. Is subclassing the right answer or do I need a different approach?
Edits:
#CarlVeazey - Sure, I call it from the viewDidLoad function.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"BETA" style:UIBarButtonItemStylePlain target:self action:#selector(betaPressed)];
Do a pull-up refactor into a UIViewController category. If your project already has one, just add this code there, otherwise press cmd-N in Xcode to create a new file and choose "Objective-C Category" and enter UIViewController in the "Category On" field.
In the interface add this method declaration:
- (void)onfConfigureRightNavigationBarButton;
And in the implementation add this method implementation:
- (void)onfConfigureRightNavigationBarButton
{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"BETA"
style:UIBarButtonItemStylePlain
target:self
action:#selector(betaPressed)];
}
Then in any UIViewController subclass you can import your category header and call this method. You may also wish to add to your category an empty implementation of betaPressed:.
BTW, ONF is the prefix I use for non-work coding so use whatever prefix already is in your project, or none at all if you're not concerned with category name collisions.

How do i create a UITextView in main UIView from a UIButton Within UIPopover?

I'm trying to create a UITextView in my Main UIViewController when a UIControlEventTouchUpInside happens with my UIButton which is in a UIPopoverController.
I tried to use a delegate protocol to allow my button to be referenced within the Main viewcontroller.m but I don't think I have been doing it right. I will also mention I've created the UIButton and UITextField programmatically in a UIView subclass for the UIPopover.
The view controller that you display in the UIPopoverController should look something like this:
#interface MyViewControllerForPopover : UIViewController
#property (weak) UIViewController *viewControllerInWhichCreateTheTextField;
...
#end
Meaning that you could keep a weak reference to the view controller in which you want to add the text field. The button you generated in this MyViewControllerForPopover could do something like:
- (void)createTextViewInConnectedViewController:(id)sender {
UITextField *myTextField = ...;
[self.viewControllerInWhichCreateTheTextView.view addSubview:myTextField];
}
The key concept is that you want to have a weak reference (to avoid retain cycle) to the view controller you wish to alter. Even better if that view controller has a method to actually add the text field itself. Hope it helps!

How to simplify code?

I'm building an very simple application and I would like to reduce the coding lines in it, and I would like to that by using one or two functions instead of 20.
The app displays 10 buttons. Each button has two buttons to display, both with the same action (sending an email) but each button has a different identity so it also has different email content. It works fine if I do a function for each button, but I know it's possible to simplify that, I just don't know how.
Can someone point me in the right direction? This is what I'm doing right now in my methods:
MFMailComposeViewController *controller1 = [[MFMailComposeViewController alloc] init];
controller1.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[controller1 setToRecipients:[NSArray arrayWithObjects: #"dev#company.com", nil]];
[controller1 setSubject:#"Button 1"];
[controller1 setMessageBody:#"The second option form button 1 was selected" isHTML:NO];
[self presentModalViewController:controller1 animated:YES];
}
[controller1 release];
Set a unique tag (see the tag property) for each button and check the tag of the sender in your one -sendEmail: method. All your buttons call that one method.

UIViewController interfaceOrientation problem

I have a Subclass of UIViewController as usual. When I load the App, I need to size some elements that I have to put in programmatically. Of course, the size depends on the interface orientation: So, I did:
- (void)viewDidLoad {
switch( [self interfaceOrientation] ) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
NSLog(#"Portrait");
break:
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
NSLog(#"Landscape");
break;
}
But no matter of the orientation of the simulator/device, the case is always Portrait in viewDidLoad/WillAppear, even if everything rotate correctly.
In the Plist I've added all supported orientations.
Hints?
Try [UIApplication sharedApplication].statusBarOrientation and check for UIDeviceOrientationPortrait, UIDeviceOrientationPortraitUpsideDown etc in your switch as [self interfaceOrientation] is unreliable in my experience.
1) Make sure that interface orientation in willAppear returns incorrect interface orientation.
2) try to look here http://developer.apple.com/library/ios/#qa/qa1688/_index.html
3) Make sure that main view controller is only viewcontroller in window (there is only one vc in window)
I was working with the rotations and orientations of the iOS App. I found that self.interfaceOrientation will always give you a correct result if called in viewDidAppear or after that.
[UIApplication sharedApplication].statusBarOrientation should be preferred when adding a view directly to UIWindow.
Hope it helps.