Resolved TextView Returning nil

Discussion in 'iOS Programming' started by dlent, Dec 11, 2011.

  1. dlent, Dec 11, 2011
    Last edited: Dec 15, 2011

    dlent macrumors newbie

    Joined:
    Dec 11, 2011
    #1
    I'm completely new to Objective C and iPhone programming. I ran my code through the debugger and my UITextView called textView appears to return nothing for [textView text] in the sendTransmit method. Does anyone see anything in my code that could be causing it? I left out the irrelevant methods.
    I'm using Xcode 4.2 with automatic reference counting.

    Code:
    //ViewController.h file
    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h>
    
    @interface MOViewController : UIViewController <UITextViewDelegate>
    {
        AVCaptureSession 	*AVSession;
        IBOutlet UITextView *textView;
    }
    
    @property (nonatomic, retain) AVCaptureSession *AVSession;
    @property (retain, nonatomic) IBOutlet UITextView *textView;
    
    - (IBAction)sendRoger:(id)sender;
    - (IBAction)sendTransmit:(id)sender;
    // -----------------------------------------
    
    // ViewController.m file
    #import "MOController.h"
    #import <AVFoundation/AVFoundation.h>
    #import <unistd.h>
    
    @implementation MOViewController
    @synthesize AVSession;
    @synthesize textView;
    
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
    {
    	if ( [ text isEqualToString: @"\n" ] ) {
    		[textView resignFirstResponder ];
    		return NO;
    	}
    	return YES;
    }
    
    - (IBAction)sendTransmit:(id)sender {
        char temp;
        
        for (int i=0; i<[[textView text] length]; i++)
        {
            // Convert to upper case character
            temp = [[[textView text] uppercaseString] characterAtIndex:i];
            
            // Send the character for transmission
            [self transmitCharacter: temp];
        }
            
    }
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    The usual reason for an IBOutlet being nil is you didn't connect it in a nib.
     
  3. dlent thread starter macrumors newbie

    Joined:
    Dec 11, 2011
    #3
    Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

    Thanks! I'll check that when I get home from work.
     
  4. dlent thread starter macrumors newbie

    Joined:
    Dec 11, 2011
    #4
    I checked the xib file and it looks like it is connected there. Any other suggestions?

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="1.0" toolsVersion="1938" systemVersion="11C74" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="2">
        <dependencies>
            <development defaultVersion="4200" identifier="xcode"/>
            <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="933"/>
        </dependencies>
        <scenes>
            <scene sceneID="5">
                <objects>
                    <placeholder placeholderIdentifier="IBFirstResponder" id="4" sceneMemberID="firstResponder"/>
                    <viewController id="2" customClass="MorseViewController" sceneMemberID="viewController">
                        <view key="view" contentMode="scaleToFill" id="3">
                            <rect key="frame" x="0.0" y="20" width="320" height="460"/>
                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                            <subviews>
                                <imageView userInteractionEnabled="NO" contentMode="bottom" image="morsecode2.png" id="ypW-SF-6GQ">
                                    <rect key="frame" x="0.0" y="0.0" width="320" height="460"/>
                                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                                    <color key="backgroundColor" red="0.5896106138618602" green="0.85526072623103611" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                </imageView>
                                <textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" id="h1U-3c-PYd">
                                    <rect key="frame" x="20" y="20" width="280" height="141"/>
                                    <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                    <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                    <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                    <textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="namePhonePad" returnKeyType="done"/>
                                    <connections>
                                        <outlet property="delegate" destination="2" id="jK2-wS-2FJ"/>
                                    </connections>
                                </textView>
                                <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="co2-XT-k5K">
                                    <rect key="frame" x="38" y="179" width="96" height="37"/>
                                    <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                    <fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
                                    <state key="normal" title="Roger">
                                        <color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
                                        <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
                                    </state>
                                    <state key="highlighted">
                                        <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                    </state>
                                    <connections>
                                        <action selector="sendRoger:" destination="2" eventType="touchUpInside" id="IhF-Eu-R2b"/>
                                    </connections>
                                </button>
                                <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="Hnq-Ss-mJh">
                                    <rect key="frame" x="184" y="179" width="96" height="37"/>
                                    <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                    <fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
                                    <state key="normal" title="Transmit">
                                        <color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
                                        <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
                                    </state>
                                    <state key="highlighted">
                                        <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                    </state>
                                    <connections>
                                        <action selector="sendTransmit:" destination="4" eventType="touchUpInside" id="kPO-OT-Dhm"/>
                                    </connections>
                                </button>
                            </subviews>
                            <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                        </view>
                        <connections>
                            <outlet property="textView" destination="h1U-3c-PYd" id="QwO-Cf-EHq"/>
                        </connections>
                    </viewController>
                </objects>
            </scene>
        </scenes>
        <resources>
            <image name="morsecode2.png" width="200" height="226"/>
        </resources>
        <classes>
            <class className="MorseViewController" superclassName="UIViewController">
                <source key="sourceIdentifier" type="project" relativePath="./Classes/MorseViewController.h"/>
                <relationships>
                    <relationship kind="action" name="sendRoger:"/>
                    <relationship kind="action" name="sendTransmit:"/>
                    <relationship kind="outlet" name="textView" candidateClass="UITextView"/>
                </relationships>
            </class>
        </classes>
        <simulatedMetricsContainer key="defaultSimulatedMetrics">
            <simulatedStatusBarMetrics key="statusBar"/>
            <simulatedOrientationMetrics key="orientation"/>
            <simulatedScreenMetrics key="destination"/>
        </simulatedMetricsContainer>
    </document>
    
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    What I highlighted in red is most likely not the issue but nonetheless, you should avoid this, at least for the sake of clarity and safety. In this case, your argument is obscuring your ivar for that method. You should give your ivar (property) a descriptive name, like "theMovieTitleField" or whatever it is. You might also consider leaving out the actual ivar declarations (using only the @property statements) since in this case they are redundant and unnecessary.

    Bad plan. This is probably your problem. Very often, something will be happening in some other code that you think is irrelevant which is actually the cause. What did you leave out? (Hint: anything that has the slightest effect on the object may be relevant.)
     
  6. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    Why do your XIB in XML code, when you can do it via Interfade builder directly >.< it's like double work ;p
    have u assigned the delegate of the text field to it's corresponding controller?
     
  7. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #7
    The XIB created by IB is an XML document. It get compiled into a NIB during the build process.
     
  8. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #8
    I know that.. But why would he paste it here :p i never actually read through the XIB, only if there are SVN merging errors.
     
  9. dlent thread starter macrumors newbie

    Joined:
    Dec 11, 2011
    #9
    Hi.

    I've included the rest of the code, but I had to censor two of the methods for security reasons. Those methods don't use textView though. Here it is:

    Code:
    #import "MOController.h"
    #import <AVFoundation/AVFoundation.h>
    #import <unistd.h>
    
    @implementation MorseViewController
    @synthesize AVSession;
    @synthesize tView;
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }
    
    -(void)setTextView:(UITextView *)textView
    {
        
    }
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
    {
    	if ( [ text isEqualToString: @"\n" ] ) {
    		[textView resignFirstResponder ];
    		return NO;
    	}
    	return YES;
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        //_textView = nil;
    }
    
    - (void)viewDidUnload
    {
        //[self setTextView:nil];
        //[self setTextView:nil];
        [self setTextView:nil];
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
    	[super viewWillDisappear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
    	[super viewDidDisappear:animated];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    
    - (IBAction)sendRoger:(id)sender { 
       // Censored
    }
    
    - (IBAction)sendTransmit:(id)sender {
        char temp;
        tView.text = @"AAA";
        /*for (int i=0; i<[[tView text] length]; i++)
        {
            // Convert to upper case character
            temp = [[[tView text] uppercaseString] characterAtIndex:i];
            
            // Send the character for transmission
            [self transmitCharacter: temp];
        }
        */    
    }
    
    - (void)transmitCharacter:(char)digit {
      
    // Censored
    
    }
    
    - (void)dealloc {
        
    }
    @end
    
    
     
  10. dlent thread starter macrumors newbie

    Joined:
    Dec 11, 2011
    #10
    I deleted the UITextView and added it again and now it works. I have no idea what was causing the problem.
     

Share This Page