I've followed the tutorial below, it basically just tells you to create a button and a label. When you click the button the label should change to some text.
http://paulpeelen.com/2011/03/17/xcode-4-ios-4-3-hello-world/
It runs but it shows a blank screen. When you click the screen it goes blue like the entire page is a button. I think it's loading the mainwindow.xib file instead of the default ViewController.xib
Any ideas, I'm obviously missing something?
Thanks
EDIT
#import <UIKit/UIKit.h>
#interface fun_buttonViewController : UIViewController {
UILabel *textLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *textLabel;
- (IBAction)changeTheTextOfTheLabel;
#end
The implementation:
#import "fun_buttonViewController.h"
#implementation fun_buttonViewController
#synthesize textLabel;
- (void)dealloc
{
[super dealloc];
[textLabel release];
}
- (IBAction)changeTheTextOfTheLabel
{
[textLabel setText:#"Hello, World!"];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
You probaly made a simple mistake. I would go through the example again and see if you did anything diffrently.
If you post your code or upload your files somewhere I could take a look at it for you.
Its kind of hard to guess what the problem is without seeing what you actually did^^
It was as I suspected. It's not a code problem, since the code is fine. The tutorial is also perfectly fine. I thought it had to do with the XIB, and when I looked into there this is what I saw:
In your MainWindow.xib, you had a button loading from the ViewController. Why? I don't know.
It might have been placed there by accident. Here's a screenshot of what I mean:
The left view is from your view controller. See the UIButton loading in the ViewController? It is blocking the rest of the view. Just delete the button that's loading OVER your view, and it works perfectly. The view hierarchy, was essentially blocked by that massive extraneous button.
It could be anything from your XIB (most likely) or from your app delegate.
Are you sure you connected the buttons properly?
Send us your code/zip of the project so far.
Related
So, I know there are similar questions to mine, but maybe not exact (so please don't mark me down -- just warn me or something). I have searched for days for the solution to this SIMPLE issue. Using storyboards, ARC, and Xcode 4.5.2, I simply need to put a bunch of labels inside a UIScrollView and have it scroll vertically. I've tried so many combinations of setting frame sizes and content sizes within viewDidLoad, viewDidAppear, and viewWillAppear, but to no avail. The scroll view scrolls perfectly when there's nothing inside of it, but when I add labels to it, the scrolling only scrolls a very short section.
Note: I need to use auto layout, otherwise my whole project will get messed up.
Here's my current code...
.h file:
#import <UIKit/UIKit.h>
#interface MortgageRatesViewController : UIViewController <UIScrollViewDelegate, UIScrollViewAccessibilityDelegate>
- (IBAction)backButton:(id)sender;
#property (strong, nonatomic) IBOutlet UIView *mortgageView;
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#end
.m file:
#import "MortgageRatesViewController.h"
#interface MortgageRatesViewController ()
#end
#implementation MortgageRatesViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//-------------------------------------------------------
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"appBackgroundColor.png"]];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(0, 809)];
}
//---------------------------------------------------------------
//---------------------------------------------------------------
//-(void)viewWillAppear:(BOOL)animated{
//
//
// [super viewWillAppear:animated];
//
//
// [self.scrollView setFrame:CGRectMake(0, 0, 320, 808)];
//
//
//}
//---------------------------------------------------------------
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.view addSubview:self.scrollView];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(0, 809)];
}
//-------------------------------------------------------------
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)backButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:NO];
}
#end
Note: having viewWillAppear commented out has made no difference.
EDIT: I POSTED THE SOLUTION BELOW. HOPE IT HELPS OTHERS!
After days of research, it's funny I actually found the solution just minutes after posting this question! So, for my situation I simply had to add this bit of code, and it worked:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.scrollView setContentSize:CGSizeMake(320, 808)];
}
It seems to be working perfectly now. Please, anyone, let me know if this is a poor way of doing it, because I'm new to this and I would love any help. Otherwise, I'll leave it and hope this can help other people! Thanks!
SOLUTION with Storyboard + Autolayout (no code):
Vertical scrolling example that starts from top-left corner:
Set the top+leading constraints of the first object (subview)
Chain up every object till the last one
The most important - set the bottom constraint to the scroll view so it's hooked up top to bottom.
Set the width of the scroll view - either a specific "magic number" or a better way - connect the trailing constraint of a subview that has a set width - if there's non I usually set a label's width = screen width - side paddings => standard(20)+UILabel(280)+standard(20) - this way the scroll view knows exactly its content's width, otherwise you'll get an "ambiguous width" warning.
*Needs constraints' adjustments if horizontal scrolling is desired.
your scrollview's frame is not the scrollable size.
Check out contentSize
Set frame to its superview.bounds then set content size to the scroolview's scrollable area.
Also I dont know that the background will scroll.
You will want to add subview's to the scrollview itself.
It should scroll its own subviews. but it will not scroll itself :)
I have been having issues forever with an app, and getting certain views to rotate.
I already know that no view in a tab bar controller can rotate, unless ALL are allowed to rotate. I also know that no view within a navigation controller can rotate unless the top most view is allowed to rotate.
I used IB mostly to setup my app.
In the MainWindow.xib I have the AppDelegate Object, Window, TabBarController, and then a separate UIViewController.
Within one of the tabs of the tab bar, I have an IBAction linked to a UIButton with the following code:
-(IBAction)stuff {
[self presentViewController:buletinss animated:YES completion:nil];
}
The view controller is declared in the header file as an IBOutlet, and is linked from that tab class to the UIViewController. In IB, I then set the class for that view controller to a UIViewController class I set up, and return YES to allow it to rotate.
However, it still will not rotate.
I thought that since it was not a part of the tab bar, and not pushed from a navigation controller, that it would be allowed to rotate, but I am having no luck. Please any help?
Here is full code:
First, the .h and .m for the view that has the button:
#import <UIKit/UIKit.h>
#interface BulletinViewController : UIViewController {
IBOutlet UIWebView *worship;
IBOutlet UIActivityIndicatorView *activity;
NSTimer *timer;
IBOutlet UIViewController *buletinss;
}
-(IBAction)stuff;
#property (nonatomic, retain) UIActivityIndicatorView *activity;
#end
and the .m
#import "BulletinViewController.h"
#implementation BulletinViewController
-(IBAction)stuff {
[self presentViewController:buletinss animated:YES completion:nil];
}
Now the .h and the .m of the view it is presenting
#import <UIKit/UIKit.h>
#interface TestBulletinViewController : UIViewController
#end
and the .m
#import "TestBulletinViewController.h"
#implementation TestBulletinViewController
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try change this code
[self presentViewController:buletinss animated:YES completion:nil];
to
[self presentModalViewController:buletinss animated:YES];
UPD: in iOS 6 you must:
1)
Replace
[window addSubview:buletinss.view];
with
window.rootViewController = buletinss;
2)
Add this code lines
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
It's hard to know what's going on without seeing your project. You're doing a very odd thing: why are you loading a view controller from a nib? You are saying:
IBOutlet UIViewController *buletinss;
Why? Why are you not explicitly instantiating a BulletinViewController and setting the ivar to that?
I'm betting that the problem is that in the nib, this object is not a BulletinViewController. It's probably just a generic UIViewController. Hence your BulletinViewController code is irrelevant; none of it ever runs. Instead, you've got a generic UIViewController that only rotates to portrait. But that's just a guess.
I have created a new project in XCode and used the new Storyboard-feature to create two different View Controllers.
The first View Controller is attached to the main files (ViewController.h, Viewcontroller.m). The second View Controller is attached to it's own set of .h/.m files (NewUserController.m/.h)
Now for the problem which I havent been able to find a solution for in the last hours;
I have added a button the second view controller and attached the button to an IBAction (verifyNumber). When I attach the 'Touched Up Inside' event the IBAction is never fired. However, when I attach the 'Touch Down' everything works fine..
Both View Controller's have got the 'user interaction enabled' selected and apart from the button the second view controller doesn't contain any other elements. Also, my manual performSegueWithIdentifier is working (switch from view1 to view2).
Can anyone spot where it has gone wrong?
The code:
ViewController.m
- (void)firstStartup {
// Future use for getting userID
// Switch to loginview
[self performSegueWithIdentifier:#"segueLogin" sender:self];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Fire firstStartup
[self firstStartup];
}
NewUserController.h
#import <UIKit/UIKit.h>
#interface NewUserController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *inputNumber;
- (IBAction)backgroundTap;
//- (IBAction)verifyNumber;
- (IBAction)verifyNumber:(id)sender;
#end
NewUserController.m
#import "NewUserController.h"
#implementation NewUserController
#synthesize inputNumber;
// Collect User data & Start Request
- (IBAction)verifyNumber:(id)sender; {
inputNumber.text = #"testing";
}
- (IBAction)backgroundTap {
[inputNumber resignFirstResponder];
}
UPDATE
Because of the response of NJones I have tested some more and deleted the gesturerecognizer I had present on the second view. After deleting this recognizer the UIButton works with all events (Touched Up Inside).
Does the recognizer somehow block any 'tap' events to overlaying objects (such as the UIButton)?
I have a few thoughts,
1) Why do you have:
- (IBAction)backgroundTap;
//- (IBAction)verifyNumber;
- (IBAction)verifyNumber:(id)sender;
There is a difference between verifyNumber and verifyNumber:(id)sender and they can both exist at the same time, and both can be connected in the nib.
2) Are you using any UIGestureRecoginzers on the view at all?
3) Is this button a custom button or subclass of UIButton?
4) (I truly don't think this will help solve your problem it's just good practice, and I'm already typing :)) Using a view property to check if a method was called is inconclusive at best. Try putting a log statement in the IBAction method like So:
- (IBAction)verifyNumber:(id)sender; {
NSLog(#"verifyNumber:");
inputNumber.text = #"testing";
}
I am a beginner in iPhone development and I have come across a problem with MKMapView. First let my describe how to reproduce the problem.
Create a view-based application
In Interface Builder, add a MapView that cover the whole view area
Add a UIButton over the Mapview (size 50x50 is fine, put it in a corner)
Associate the MapView in IB with a MapView reference in the code
Associate the UIButton to an handling method (touch down).
Here's the code for the controller (MapTest3ViewController.h)
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MapTest3ViewController : UIViewController {
MKMapView *myMapView;
}
#property (nonatomic,retain) IBOutlet MKMapView *myMapView;
- (IBAction) zoomClicked:(id)sender;
#end
(MapTest3ViewController.m)
#import "MapTest3ViewController.h"
#implementation MapTest3ViewController
#synthesize myMapView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//myMapView.zoomEnabled = NO;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
- (IBAction) zoomClicked:(id)sender
{
// when zoom button is clicked, zoom on a specific region
CLLocationCoordinate2D locationCoordinate2D;
locationCoordinate2D.latitude = 35.6994;
locationCoordinate2D.longitude = 139.78;
MKCoordinateSpan coordinateSpan;
coordinateSpan.latitudeDelta = 1;
coordinateSpan.longitudeDelta = 1;
MKCoordinateRegion coordinateRegion;
coordinateRegion.center = locationCoordinate2D;
coordinateRegion.span = coordinateSpan;
[myMapView setRegion:coordinateRegion animated:YES];
}
#end
I have set scrollEnabled=YES, zoomEnabled=YES and showUserLocation=YES in IB. The application WORKS fine. I can zoom using pinching, I can scroll, whenever I push the "Zoom" button the view area moves to the region defined in zoomClicked.
The problem
When I set zoomEnabled=NO in viewDidLoad, I can no longer zoom using pinching gestures which is fine. However, if I "play" with the map, scrolling, trying to zoom(which of course doesn't work), then push the zoom button the map freezes and I can no longer scroll or do anything.
Again, how to replicate the problem (zoomEnabled=NO)
After application loads, the map of the entire earth appears. Do some pinching gestures in and out, scroll the map. (Zoom doesn't work which is expected)
Push the zoom button which should set the map the a specific region (in the code Tokyo, Japan) -> the map freezes.
My setup
iPhone OS 4.0.1/iPhone 3GS/XCode 3.2.3 64bit/Testing done on the phone itself.
Any help would be greatly appreciated.
Thanks.
I am having difficulty geting a very simple view to display. This view has a custom view controller that is manages by a switching view controller. The XIB has one UIViewController component on it with its Image property set. The view controller is as follows:
InstructionsViewController.h
#import <UIKit/UIKit.h>
#interface InstructionsViewController : UIViewController {
}
#end
InstructionsViewController.m
#import "InstructionsViewController.h"
#implementation InstructionsViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
#end
I have also set the class property for the XIB's File's Owner to InstructionsViewController, and Ctrl+Dragged File's Owner to the View icon and selected View from the popup menu.
The code to display the view is:
- (void) showInstructions
{
//Lazy load the instruction view.
if (self.instructionsViewController == nil)
{
InstructionsViewController *viewController = [[InstructionsViewController alloc]
initWithNibName:#"InstructionsView"
bundle:nil];
self.instructionsViewController = viewController;
[viewController release];
}
[self.view insertSubview:viewController.view atIndex:0];
}
My view controller that animates different views when they are switched, has no problems loading and displaying three other views. It just doesn't like this one.
I'm sure I've missed something simple, but for the life of me I can't get it to work for some reason. Does anyone have any pointers?
Thanks,
Steve
I just had the same thing. Ended up deleting the UIImageView, placing a new one then relinked and it worked. Seems like some corruption somewhere.
Not even really sure how this is compiling. You are inserting the subview outside of your nil check, where viewController is out of scope.
You probably want to do
[self.view insertSubview:self.instructionsViewController.view atIndex:0];
Have you verified that showInstructions is being called by setting a breakpoint there? One other thing to check is that you are inserting the Instructions view at the bottom of your z-order, so it will be behind all other items. Try using:
[self.view addSubview:self.instructionsViewController.view];
This will add it to the top of the z-order. If that doesn't work, also try:
[self.view setNeedsLayout];
and/or:
[self.instructionsViewController.view setNeedsLayout];
Ok, I've finally solved it!
The problem was something in XCode or Interface Builder was not agreeing with the PNG file I specified for the Image View control in IB. I tried re-naming, and re-specifying, etc. to no avail.
I finally had to delete the graphic and re-copy it from it's source location. Then I re-specified it in Image View, build and ran. Now it works.
Everything is good in the world again.
Thanks to those who took the time to read and offer help. I appreciate it very much!
Steve